com.example.helloandroid.R.id cannot be resolved
I followed the Hello, Testing tutorial to the letter.
Yet, the following line produces a "com.example.helloandroid.R.id cannot be resolved" error:
mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);
Eclipse amazingly suggests 2 quick fixes:
- Create field 'id' in type 'R'
- Create constant 'id' in type 'R'
Can you help me understand what these fix开发者_如何学Ces mean? Are these really the correct fixes? (why didn't the tutorial provide them, too?)
In HelloAndroid project, HelloAndroid.java
setContentView(R.layout.main);
Check your HelloAndroid project, in "gen->com.example.helloandroid->R.java" have the following code like:
public static final class id {
public static final int textview=0x7f050000;
}
if no, check "res->layout->main.xml". there is "android:id="@+id/textview" as follows?
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
check these items, fix your HelloAndroid.java and/or main.xml if needed. rebuild HelloAndroid project, and rebuild HelloAndroidTest project.
Are these really the correct fixes?
None of them.
What you have to do is importing the correct R
class. Sometimes eclipse help you with that when you press Ctrl+Shift+O.
I am not an expert. But I can tell you that this problem also happened to me. Thanks to the comments above, I figured out that between the time I worked the Hello World tutorial and the time that I tried the Hello World Testing tutorial, I had tried the Linear Layout tutorial in the "Hello, Views" section. Following the Linear Layout tutorial, I changed main.xml.
I restored main.xml to the way in which the Hello World tutorial expected it and that solved the missing "id" problem.
Bit of a noob myself but recently came across this (or a similar) problem. You shouldn't have to import your R class because you give it an fully-qualified package name (com.yourpackage.R...) in the findviewById and the R class is public. However, if like me you had put both your project and your test project in the same package you are probably referencing your test R class not your project's R class.
Changing my test manifest to be com.mypackage.test instead of com.mypackage (and moving any subsequent misplaced classes to the new com.mypackage.test) fixed the problem (you may have to delete your test R class for it to be regenerated)
I had this problem as well while following the Android training for creating tests. None of the above solutions worked - a R.java class was continuously created within the MyFirstAppTest project, and this is the R class that was picked up in the MainActivityTest class. The only way that I could resolve it was to import the R class from the project under test (in my MainActivityTest.java file):
import com.example.myfirstapp.R;
Then the correct R class was picked up along with its id method.
精彩评论