1

我设置了一个测试项目来测试另一个 Android 项目。一切正常。我可以打电话给 solo.clickOnView 并且测试会运行良好。当我尝试更新视图时会出现问题,例如,我想通过调用 solo.enterText 来更新 EditText。

机器人 3.6 版

String hello="Hello world"
solo.enterText(myEditText, hello);

我确定 myEditText 是一个非空对象。运行测试,将出现以下消息

错误信息

java.lang.NullPointerException
at android.app.Instrumentation.runOnMainSync(Instrumentation.java:338)
at com.jayway.android.robotium.solo.TextEnterer.setEditText(TextEnterer.java:52)
at com.jayway.android.robotium.solo.Solo.enterText(Solo.java:1404)
at com.darakok.test.TestMain.testDisplayBlackBox(TestMain.java:30)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
4

4 回答 4

0

请参阅 API 文档,此方法仅适用于元素的索引。所以如果你想使用id:

String hello="Hello world";
solo.typeText(myEditText, hello);
于 2012-11-22T20:05:56.713 回答
0

在执行代码之前尝试做一个额外的检查:

solo.enterText(myEditText, hello);  // your code

您可以编写以下快照代码:

ArrayList<View> views = solo.getViews();
for (View view : views) {
    // check if your view is on the views arraylist, use a break point o whatever
}

也许当您尝试修改视图时,它没有附加到布局上,或者它可能不可编辑。

于 2015-04-02T13:04:00.400 回答
0

您可能尚未初始化对象。只需确保您正在使用的对象已被 初始化

于 2012-11-28T06:20:02.317 回答
0

也许 myEditText 实际上并没有分配给正确的 EditText?

String hello = "Hello world";    

//Based on the ID of the EditText you've given via the layout XML: 
EditText myEditText = (EditText) tester.getView(R.id.my_edit_text);
solo.enterText(myEditText, hello);

发生这种情况的另一个可能原因是在myEditText整个 Activity(甚至只是 EditText 本身)正确加载之前执行的块。在这些情况下,我建议assertCurrentActivity(), sleep() or waitForView()

于 2012-11-28T03:05:51.723 回答