我有一个ListView。 ListView的Items保存在名为MSG的ArrayList中。 现在我将onSaveInstanceState和onRestoreInstanceState实现到我的类。
方向更改的工作原理,但当我点击ListView的项目时,应用程序崩溃。
我不知道问题可能是什么。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
//ListView
lv = (ListView)findViewById(R.id.list2);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
try {
Intent ii = new Intent(Inbox.this, MSGsOpenMsg.class);
ii.putExtra("messageid", m_MSGs.get(position).messageid);
ii.putExtra("box", "inbox");
startActivityForResult(ii, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putSerializable("MSGs", (Serializable)m_MSGs);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
m_MSGs = (ArrayList<MSGs>) savedInstanceState.getSerializable("MSGs");
}
答案 0 :(得分:0)
如果您的ListView类正在扩展android api ListActivity,我处理单击或选择其中一个列表项的方法是通过覆盖onListItemClick方法,如下所示:
/**
* onListItemClick method
*
* This method will be called when an item in the list is selected.
*
* Subclasses should override. Subclasses can call
* getListView().getItemAtPosition(position) if they need to access the data
* associated with the selected item.
*
* @param ListView
* l The ListView where the click happened.
* @param View
* v The view that was clicked within the ListView.
* @param int position The position of the view in the list.
* @param long id The row id of the item that was clicked.
*
* @return void
*
*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
if (this.mListCursor != null) {
this.mListCursor.moveToPosition(position);
Intent listItemIntent = new Intent(this, otherAppActivity.class);
//You can put whatever you want here as an extra
listItemIntent.putExtra("listItemValue", this.mListCursor
.getString(this.mListCursor
.getColumnIndexOrThrow(myDbAdapter.KEY_TABLE_PRIMKEY)));
startActivity(listItemIntent);
}// end if (this.mListCursor != null)
} catch (Exception error) {
MyErrorLog<Exception> errExcpError = new MyErrorLog<Exception>(this);
errExcpError.addToLogFile(error,
"myListView.onListItemSelected", "no prompt");
errExcpError = null;
}// end try/catch (Exception error)
}// end onListItemClick