Reputation: 681
Android Studio 2.1, Java V8 Update 91, Gradle 2.1.0 .
I know that this questions has been asked many times here, but none of the recommendation solutions works out.
In a sudden after compiling ready to run on Emulator, my IDE give an error at the R.id attribute. Every Java files suddenly give the same error even I don't make any changes to the file.
I tried to make a new project, see if things different but the error keep coming. In my mind it could be internal error, again. Even I just update my IDE just now.
The Java file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science);
list_View = (ListView) findViewById(R.id.maintable);
String[] Days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
dayList.addAll(Arrays.asList(Days));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
list_View.setAdapter(adapter);
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
if (nfc.isEnabled()) {
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
Toast.makeText(this, "NFC turned ON", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Please turn On NFC", Toast.LENGTH_SHORT).show();
}
}
The R
is the error.
XML file that associate to the Java;
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/maintable"
>
</ListView>
Is this, again, an internal error?
Upvotes: 4
Views: 24638
Reputation: 1301
Try syncing the project, go to Tools > Android > Sync Project with Gradle Files
UPDATE
If you're using Android Studio 3.3, use the Sync Project with Gradle Files
icon
Upvotes: 12
Reputation: 360
I've seen this problem many times. The right solution is:
When you type R
, the Android Studio will give you many different namespaces for R
to choose from, that look like R(***.***.***)
, but what you really need is the namespace that ends with ***.myProject
. (Note: myProject is your project name), e.g: R(com.example.milka.demoProject)
.
If you choose any other namespace, you will get the error message, so you have to delete the unneeded import
, and re-choose your R
Upvotes: 1
Reputation: 21
I have just gone through this nightmare. Ended up removing all xml code to notepad until it was fixed. Then reintroduced sections of code until problem appeared. Thinking back, it all started when I pasted some code using WordPad. Apparently WordPad contains a different coding or something so bet to avoid. Anyway just check your white space in xml by deleting it all to rid yourself of any hidden characters.
Upvotes: 2
Reputation: 1655
Sometimes the R.java class is not created when there is some error into an xml file (like activity_main.xml). For example, if you change some attribute name that has a reserved word that cannot be changed.
Like this: if you changed the attribute called: id to another name like
id_A_Word_With_Error_Here in this code that can be part of the activity_main.xml, your error (R class not created) can occur.
<android.support.v7.widget.RecyclerView
android:id="@+id/messageRecyclerView"
and there is an error like:
<android.support.v7.widget.RecyclerView
android:id_A_Word_With_Error_Here="@+id/messageRecyclerView"
If clean, rebuild did not work and there is no error with import like import com.something...
, my suggestion is to search an error into xml files.
Upvotes: 1
Reputation: 11
Have you tried Rebuild Project go to Build> Rebuild Project and perform it without doing the Clean project I had the same problem & it worked for me
Upvotes: -1
Reputation: 41
Maybe the error may cause for the tools, trying install Android SDK Build Tools from Tools > Android > SDK Manager. or maybe this error can be caused by adding an activity to a namespace that is different to the root, you can trying >>Import com.example.MyApplication.R; or your also can clean o rebuild your project in Build > Clean Project or Build > Rebuild Project
Upvotes: 0
Reputation: 681
I try to change the Gradle version from 2.1.0 to 2.0.0, even in AS ver 2.1, ignoring the Instant Run performance and its work out. The little R error is now gone. Now, I tried to change back to 2.1.0, the R error also gone..!! Sometime this little thing drive me crazy, a bit.
Shame on me.
Upvotes: 0
Reputation: 3
If you are using google play services and your compilesdk version is lower than 23 then download the new sdk and update your compile version. After that try cleaning and rebuilding. It will be fixed
Upvotes: -1
Reputation: 336
Try performing a clean and rebuild by going to Build > Clean Project
and Build > Make Project
Upvotes: 1