Eclipse not Recognizing “R.id…” with Android

2020-07-08 07:48发布

Any ideas why at times Eclipse frizzles and doesn't recognize code it did seconds ago? The "R" in the code below has the red squiggle under it when it didn't 5 minutes ago and I've change no code. This happens frequently with Eclipse and usually I shut it down and start it again and it's good to go...no go this time.

// Initialize view elements
    mSearchBox      = (EditText) this.findViewById(R.id.edittext_search);
    mSearchButton   = (Button) this.findViewById(R.id.button_search);

8条回答
趁早两清
2楼-- · 2020-07-08 07:55

it sounds like the R class needs to be regenerated, try making a change to the layout file and saving it so the project rebuilds.

查看更多
姐就是有狂的资本
3楼-- · 2020-07-08 07:58

As of Android Development Tool (ADT) 14, the resource fields such as R.id. are no longer constants when defined in the library project. This is necessary to make library projects reusable without recompiling them.

There is an easy fix for R.id. that you know is correct but Android thinks it is incorrect. (Assuming that your .xml file is correct, your java code references the correct resource, and you DO NOT have the "import android.R;" statement)

  1. Position the cursor on the switch statement and right click on it.
  2. Select QUICK FIX. Eclipse will convert your switch staments into if-else if-else statements.
  3. This should fix the R.id. issue.
查看更多
相关推荐>>
4楼-- · 2020-07-08 08:00

I ran into the same problem. All I did was erase the "import android.R" statement and Eclipse automatically gave me the imports that I needed. Once I hit save, all my errors went away. I struggled for three days trying to figure this out.

BTW - make sure you do this with all the files.

查看更多
霸刀☆藐视天下
5楼-- · 2020-07-08 08:01

In addition to the answers above, this error can occur if you haven't declared the package in the AndroidManifest.xml, or you have the wrong package declared. Ensure that you have

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.applicationname" ...
查看更多
聊天终结者
6楼-- · 2020-07-08 08:05

Did you add any image files to your drawable folder? Image files with invalid file names can also cause the same issue.

查看更多
做个烂人
7楼-- · 2020-07-08 08:08

This is Very common problem in Android just follow this,

  • Go to Project Menu
  • Selection "clean" option.
  • Make sure "Build Automatically Option is checked"

done, your R.java's error will get resolve.

查看更多
登录 后发表回答