开发者

Android Programming Error

开发者 https://www.devze.com 2023-01-26 04:11 出处:网络
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

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();        
     开发者_如何学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"
/>
0

精彩评论

暂无评论...
验证码 换一张
取 消