2010-11-22 52 views
3


我正在使用Eclipse Galileo进行Android“Hello,Testing”教程。 (http://developer.android.com/resources/tutorials/testing/helloandroid_test.html)当我尝试编译并运行程序时,出现错误,提示“com.example.helloandroid.R.id无法解析”。Android编程错误

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

感谢您提供任何帮助/建议!

+2

请发表你的全部代码。您可能错过了导入。 – birryree 2010-11-22 21:01:26

回答

4

这种事情似乎是随机发生的,即使没有任何事情做错了,所以首先尝试清理你的项目,以强制完成R.java的重建和再生。

如果这样不能解决问题,您可能需要重新开始,并确保您完全按照项目设置说明进行操作。你对com.example.helloandroid.R的明确引用要求你的项目被命名,而不是com.example.HelloAndroidTest,因为如果这是你的主类,它可能会结束。如果打开gen /文件夹并查看不在com.example.helloandroid包中的.R.java,那就是您的问题 - 生成的R类的包以及根据需要引用的绝对或相对名称匹配。

+0

谢谢!我会试一试! – androidGM 2010-11-22 21:08:36

1

编辑HelloAndroid项目的R.java(发现于gen)而不是HelloAndroidTest。找到这个块

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
/> 

这里的问题是没有指定id。只需添加

android.id:"@+id/textview" 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
/>