3


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!

1
  • 2
    Please post your entire code. You are probably missing an import. Commented Nov 22, 2010 at 21:01

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I also noticed that it's giving me quick fixes: create field type 'id' in type 'R' and create constant 'id' in type 'R'
Sounds like you may have an xml file missing or associated with the wrong package
It turns out that the id of the TextView was never identified in the main.xml of my Hello Android project so there was no id created in com.example.helloandroid.R. Thanks for your help!
1

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"
/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.