0

我是 android 新手,我想在 android 中进行 junit 测试,然后我从 android 网站遵循了这段代码

我收到很多错误,因为HelloAndroid 无法解析为一种类型

此行有多个标记 - HelloAndroid 无法解析为类型 - 构造函数 ActivityInstrumentationTestCase2(Class) 指的是缺少的类型 HelloAndroid

此行有多个标记 - ActivityInstrumentationTestCase2 类型的方法 getActivity() 指的是缺少的类型 HelloAndroid - HelloAndroid 无法解析为类型

此行有多个标记 - HelloAndroid 无法解析为类型 - com.example.helloandroid.R 无法解析为变量

 package  com.example.helloandroid.test;

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

谁能帮我

4

2 回答 2

0

评论中的空间太小,无法向您展示我的观点...

您缺少的一件事(查看您的代码)是导入部分:

您的导入部分:

import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

教程导入部分:

import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

教程还说:

本教程及其代码依赖于 Hello World 教程。如果您尚未完成该教程,请立即完成。您将学习 Android 应用程序开发的基础知识,并且您将拥有一个可供测试的 Android 应用程序。本教程将指导您使用 ADT Plugin for Eclipse 和其他 SDK 工具设置 Android 测试项目。您将需要一个版本为 1.5(API 级别 3)或更高版本的 SDK 开发平台。

确保您首先完成了本教程。将您的 HelloAndroid 类与HelloWorld 教程中的最终版本进行比较。快乐编码!

于 2012-06-01T07:30:32.657 回答
0

我尝试了对我有用的相同示例

我猜在代码中

 public HelloAndroidTest() {
    super("com.example.helloandroid", HelloAndroid.class);
    }

而不是HelloAndroid.class 你应该把类名放在我的例子中,而是MainActivity 我把它改成了它MainActivity.class并且它有效

检查您正在测试的类并将 更改<HelloAndroid>为类名i.i MainActivity

于 2014-02-26T06:34:27.550 回答