【问题标题】:Runtime Exception in Android JUnit testingAndroid JUnit 测试中的运行时异常
【发布时间】:2011-03-22 16:07:57
【问题描述】:

我有一个简单的 HelloWorld Activity,我尝试使用 Android JUnit 测试对其进行测试。应用程序本身运行正常,但测试失败并显示

“java.lang.RuntimeException:无法解析活动:Intent { action=android.intent.action.MAIN flags=0x10000000 comp={no.helloworld.HelloWorld/no.helloworld.HelloWorld} } 在 no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"

这是我的活动课:

包号.helloworld;

导入android.app.Activity;进口

android.os.Bundle;

公共类 HelloWorld 扩展 活动{

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
} }

还有测试:

公共类 HelloWorldTestcase 扩展 ActivityInstrumentationTestCase2 {

private HelloWorld myActivity;
private TextView mView;
private String resourceString;
public HelloWorldTestcase() {
    super("no.helloworld.HelloWorld", HelloWorld.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    myActivity = this.getActivity();
    mView = (TextView) myActivity.findViewById(no.helloworld.R.id.txt1);
    resourceString = myActivity
            .getString(no.helloworld.R.string.helloworld);
}

public void testPreconditions() {
    assertNotNull(mView);
}

public void testText() {
    assertEquals(resourceString, (String) mView.getText());
}

protected void tearDown() throws Exception {
    super.tearDown();
}

为什么测试失败? Activity(当然)在 AndroidManifest.xml 中定义,应用程序按应有的方式运行。

【问题讨论】:

    标签: android exception junit android-activity android-intent


    【解决方案1】:

    构造函数调用中的包应与清单中的检测目标相匹配。它应该是“no.helloworld”而不是“no.helloworld.HelloWorld”

    【讨论】:

    • “构造函数调用中的包应该与清单中的检测目标匹配” - 这对我有帮助。我花了几个小时来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2012-08-07
    • 2015-05-29
    • 2014-02-22
    • 2011-01-28
    相关资源
    最近更新 更多