package com.example.helloandroid.test;
import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid>
{
private HelloAndroid mActivity; // the activity under test
private TextView mView; // the activity's TextView (the only view)
private String resourceString;
public HelloAndroidTest()
{
super("com.example.helloandroid", HelloAndroid.class);
}
@Override
protected void setUp() throws Exception
{
super.setUp();
mActivity = this.getActivity();
开发者_如何学C mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);
resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);
}
public void testPreconditions()
{
assertNotNull(mView);
}
public void testText()
{
assertEquals(resourceString,(String)mView.getText());
}
}
Thanks for any help/advise you can offer!
This kind of thing seems to randomly happen sometimes even without anything done wrong, so first try cleaning your project to force a complete rebuild and regeneration of R.java.
If that doesn't fix it, you may want to start over and be very sure you follow the instructions for project setup exactly. Your explicit reference to com.example.helloandroid.R requires that your project be named that, and not com.example.HelloAndroidTest as it might end up if that's your main class. If you open up the gen/ folder and see an .R.java that isn't in the com.example.helloandroid package that's your problem - the package of the generated R class and the absolute or relative name you refer to it by need to match.
Edit R.java (found in gen) of HelloAndroid project not HelloAndroidTest. Find this block
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
Here the issue is there is no id specified. just add
android.id:"@+id/textview"
i.e
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
精彩评论