android应用程序崩溃在onSaveinstanceState开始intent
问题描述:
我有一个ListView。 ListView的项目保存在名为MSG的ArrayList中。 现在我已经为我的类实现了onSaveInstanceState和onRestoreInstanceState。android应用程序崩溃在onSaveinstanceState开始intent
方向改变的作品,但当我点击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");
}
答
如果您的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
+0
这个问题发生不。问题,onClick事件的作品,但只有当我添加实例的代码时,它会崩溃 – Stefan
你可以提供一个堆栈跟踪? –
欢迎来到Stackoverflow!如果您发现回复有帮助,请投票。如果回复成功回答您的问题,请点击旁边的绿色复选标记以接受答案。也请看看http://stackoverflow.com/questions/how-to-ask关于如何写一个好问题的建议 –
当它尝试启动意图= /但是我不知道为什么 – Stefan