我已經實現了一個ListView
,當點擊一個列表項時,將啓動一個新的Activity
。當我手動測試時,它完美地工作。但是當我嘗試使用ActivityUnitTestCase
進行自動測試時,我得到一個NullPointerException
,好像ListView
是空的。使用ActivityUnitTestCase測試Android ListView的點擊
MainActivity(部分):
public class MainActivity extends ListActivity {
@Override
protected void onResume() {
super.onResume();
String[] items = new String[] {"item 1", "item 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView clickedItem = (TextView) view;
CharSequence clickedItemText = clickedItem.getText(); // throws a NullPointerException when running testWithPerformItemClick()!
Intent intent = new Intent(MainActivity.this, ItemDisplayActivity.class);
intent.putExtra("parameter", clickedItemText);
startActivity(intent);
}
});
}
}
測試代碼失敗:
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
ListView listView;
View child0;
public MainActivityTest() {
super(MainActivity.class);
}
private void setUpTest() {
MainActivity activity = startActivity(new Intent(), null, null);
getInstrumentation().callActivityOnStart(activity);
getInstrumentation().callActivityOnResume(activity);
listView = (ListView) activity.findViewById(android.R.id.list);
child0 = listView.getChildAt(0); // returns null!
}
public void testWithPerformClick() {
setUpTest();
child0.performClick(); // throws a NullPointerException!
Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}
public void testWithPerformItemClick() {
setUpTest();
long itemId = listView.getAdapter().getItemId(0);
listView.performItemClick(child0, 0, itemId); // throws a NullPointerException!
Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}
兩種測試方法失敗,因爲listView.getChildAt(0)
返回null
。爲什麼ListView
是空的?我如何強迫它用正確的孩子更新自己?
你試過['performItemClick()'](http://developer.android.com/reference/android/widget/AdapterView.html#performItemClick(android.view.View,%20int,% 20long))? – Ibungo
使用'performItemClick()',代碼會變得更遠一些,直到'onItemClick'方法的'clickedItem.getText()'部分。一旦出現,因爲提供的'view'爲'null',因爲getChildAt(0)返回null,所以引發NPE。更新問題描述以使其更清楚。 – lacton