我正在尝试编写一个测试,用于检查在更改设备方向时是否保留选择。问题是ListView
在更改后没有孩子。有没有人对如何修复此测试有任何建议?
public void testSelectionAfterSaveInstanceState() throws Throwable {
Log.d(TAG, "testSelectionAfterSaveInstanceState()");
ListView lv = ((ListActivity) this.activity).getListView();
ArrayList<Integer> indexes = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) {
int cardIndex = (int) (Math.random() * (lv.getChildCount() - 1) + 1);
final CheckedTextView ctv = (CheckedTextView) lv.getChildAt(
cardIndex).findViewById(R.id.checkmark);
if (!ctv.isChecked()) {
indexes.add(cardIndex);
this.runTestOnUiThread(new Runnable() {
@Override
public void run() {
Assert.assertTrue(ctv.performClick());
}
});
}
}
Log.d(TAG, "change orientation");
this.activity
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(TAG, "assertions");
lv = ((ListActivity) this.activity).getListView();
Log.d(TAG, "lv.getChildCount()=" + lv.getChildCount()); // 0
for (int i = 0; i < indexes.size(); i++) {
View row = lv.getChildAt(indexes.get(i));
Log.d(TAG, "row=" + row); // null
CheckedTextView ctv = (CheckedTextView) row
.findViewById(R.id.checkmark); // NPE
Assert.assertTrue(ctv.isChecked());
}
Log.d(TAG, "finished");
}
由于受欢迎的需求,这里是精确的堆栈跟踪:
java.lang.NullPointerException
at bbct.android.common.activity.test.BaseballCardListWithDataTest.testSelectionAfterSaveInstanceState(BaseballCardListWithDataTest.java:513)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
此外,正在测试ListActivity
的{{3}}。