Android Programming Error

2019-07-07 03:04发布


I am doing the Android "Hello, Testing" Tutorial using Eclipse Galileo. (http://developer.android.com/resources/tutorials/testing/helloandroid_test.html) When I try to compile and run the program I get an error saying "com.example.helloandroid.R.id cannot be resolved".

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();        
        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!

2条回答
我只想做你的唯一
2楼-- · 2019-07-07 03:46

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"
/>
查看更多
Melony?
3楼-- · 2019-07-07 03:50

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.

查看更多
登录 后发表回答