I was working on my app and everything was normal until I tried to display image in java.
I ran the app once and it ran normally, the picture was displayed. After that it asked me to import some libraries and I imported them. After that I got errors for my activities.
Errors like:
Gradle: error: cannot find symbol variable activity_main
Gradle: error: cannot find symbol variable button1
Gradle: error: cannot find symbol variable button2
Gradle: error: cannot find symbol variable textView
Gradle: error: cannot find symbol variable secondActivity
In MainActivity I have imported these libraries:
import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
and in secondActivity these:
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Does anyone know how to fix this?
EDIT: I deleted import android.R;
and now it works normally.
You shouldn't be importing android.R
. That should be automatically generated and recognized. This question contains a lot of helpful tips if you get some error referring to R
after removing the import.
Some basic steps after removing the import, if those errors appear:
[a-z0-9.]
. Capitals or symbols are not allowed for some reason.If you are using multiple flavors?
-make sure the resource file is not declared/added both in only one of the flavors and in main.
Example: a_layout_file.xml
file containing the symbol variable(s)
src:
flavor1/res/layout/(no file)
flavor2/res/layout/a_layout_file.xml
main/res/layout/a_layout_file.xml
This setup will give the error: cannot find symbol variable, this is because the resource file can only be in both flavors or only in the main.
If you are using a String
build config field in your project, this might be the case:
buildConfigField "String", "source", "play"
If you declare your String like above it will cause the error to happen. The fix is to change it to:
buildConfigField "String", "source", "\"play\""
Open project in android studio click file and click Invalidate Caches/Restart
make sure that the imported R is not from another module. I had moved a class from a module to the main project, and the R was the one from the module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With