正如标题一样,我必须对带有列表的对话框进行 Junit 测试。我想通过这种方式单击该项目,但它不起作用。我得到了对话框的位置并发送了一个触摸事件。
Rect outRect = new Rect();
dialogTmp.getWindow().getDecorView().getHitRect(outRect);
int x = outRect.left + ((outRect.right - outRect.left) / 2);
int y = outRect.top + ((outRect.bottom - outRect.top) / 2);
MotionEvent ev = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
(float) x, (float) y, (float) 0.0125, 0, 0, 0, 0, 4, 0);
dialogTmp.dispatchTouchEvent(ev);
ev.setAction(MotionEvent.ACTION_UP);
dialogTmp.dispatchTouchEvent(ev);
我得到了这个错误:
09-18 13:35:31.211: I/TestRunner(16493): ----- begin exception -----
09-18 13:35:31.211: I/TestRunner(16493): junit.framework.AssertionFailedError: java.lang.NullPointerException
09-18 13:35:31.211: I/TestRunner(16493): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:829)
09-18 13:35:31.211: I/TestRunner(16493): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1755)
09-18 13:35:31.211: I/TestRunner(16493): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1152)
09-18 13:35:31.211: I/TestRunner(16493): at android.app.Dialog.dispatchTouchEvent(Dialog.java:642)
09-18 13:35:31.211: I/TestRunner(16493): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 13:35:31.211: I/TestRunner(16493): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestCase.runBare(TestCase.java:127)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult$1.protect(TestResult.java:106)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult.runProtected(TestResult.java:124)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult.run(TestResult.java:109)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestCase.run(TestCase.java:118)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
09-18 13:35:31.211: I/TestRunner(16493): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1473)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.Assert.fail(Assert.java:47)
09-18 13:35:31.211: I/TestRunner(16493): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 13:35:31.211: I/TestRunner(16493): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestCase.runBare(TestCase.java:127)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult$1.protect(TestResult.java:106)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult.runProtected(TestResult.java:124)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestResult.run(TestResult.java:109)
09-18 13:35:31.211: I/TestRunner(16493): at junit.framework.TestCase.run(TestCase.java:118)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
09-18 13:35:31.211: I/TestRunner(16493): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
09-18 13:35:31.211: I/TestRunner(16493): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1473)
09-18 13:35:31.211: I/TestRunner(16493): ----- end exception -----
任何提示将不胜感激。谢谢!