我在点击 listView 项目时遇到异常..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new MyAdapter(this,R.layout.list_item);
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adaptr, View arg1, int position, long arg3) {
Times times = (Times)adaptr.getItemAtPosition(position);
//Times times = (Times)MyXMLHandler.array.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(times.getDescription());
builder.setTitle("Description");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
}
错误.......
06-20 14:55:13.137: ERROR/AndroidRuntime(848): FATAL EXCEPTION: main
06-20 14:55:13.137: ERROR/AndroidRuntime(848): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
06-20 14:55:13.137: ERROR/AndroidRuntime(848): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-20 14:55:13.137: ERROR/AndroidRuntime(848): at java.util.ArrayList.get(ArrayList.java:311)
06-20 14:55:13.137: ERROR/AndroidRuntime(848): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
06-20 14:55:13.137: ERROR/AndroidRuntime(848): at android.widget.AdapterView.getItemAtPosition(AdapterView.java:740)