Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android应用程序在onSaveinstanceState启动时崩溃_Android_Listview_Save_Instance_State - Fatal编程技术网

android应用程序在onSaveinstanceState启动时崩溃

android应用程序在onSaveinstanceState启动时崩溃,android,listview,save,instance,state,Android,Listview,Save,Instance,State,我有一个列表视图。ListView的项保存在名为MSGs的ArrayList中。 现在我在类中实现了onSaveInstanceState和onRestoreInstanceState 方向改变的东西可以工作,但当我点击ListView的一个项目时,应用程序崩溃了 我不知道可能是什么问题 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我有一个列表视图。ListView的项保存在名为MSGs的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");
 }
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
//列表视图
lv=(ListView)findViewById(R.id.list2);
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、,
内部位置,长id){
试一试{
意向ii=新意向(Inbox.this,MSGsOpenMsg.class);
ii.putExtra(“messageid”,m_MSGs.get(position.messageid);
ii.额外(“盒子”、“收件箱”);
startActivityForResult(ii,0);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
@凌驾
SaveInstanceState上的公共无效(Bundle savedInstanceState)
{
savedInstanceState.putSerializable(“MSGs”,(可序列化)m_MSGs);
super.onSaveInstanceState(savedInstanceState);
}
@凌驾
RestoreInstanceState上的公共无效(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
m_MSGs=(ArrayList)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
/**
*onListItemClick方法
* 
*选择列表中的项目时,将调用此方法。
* 
*子类应该重写。子类可以调用
*getListView().getItemAtPosition(位置),如果他们需要访问数据
*与所选项目关联。
* 
*@param ListView
*l发生单击的列表视图。
*@param视图
*v在ListView中单击的视图。
*@param int position视图在列表中的位置。
*@param long id单击的项目的行id。
* 
*@返回无效
* 
*/
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
试试{
如果(this.mListCursor!=null){
此.mListCursor.moveToPosition(位置);
Intent listItemIntent=新的意图(this,otherAppActivity.class);
//你可以把你想要的任何东西放在这里作为额外的
listItemIntent.putExtra(“listItemValue”,this.mListCursor)
.getString(this.mListCursor
.getColumnIndexOrThrow(myDbAdapter.KEY\u TABLE\u PRIMKEY));
startActivity(listItemIntent);
}//如果(this.mListCursor!=null)结束
}捕获(异常错误){
MyErrorLog errexperor=新的MyErrorLog(此);
errexPeror.addToLogFile(错误,
“myListView.onListItemSelected”,“无提示”);
errexperor=null;
}//结束尝试/捕获(异常错误)
}//仅限结束列表项单击

能否提供堆栈跟踪?欢迎使用Stackoverflow!如果你觉得回复有帮助,请投票表决。如果回答成功回答了您的问题,请单击旁边的绿色复选标记以接受回答。另外,请查看如何编写一个好的问题的建议当它尝试启动Intent=/时会出现问题,但我不知道为什么操作不是问题,onClick事件可以工作,但只有当我添加instancestate的代码时才会发生save it crash