ちょうどタイトルのように、リスト付きのダイアログについて 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 -----
ヒントをいただければ幸いです。ありがとうございました!