【问题标题】:android app crash at starting intent with onSaveinstanceStateandroid 应用程序在使用 onSaveinstanceState 启动意图时崩溃
【发布时间】:2011-11-16 20:26:30
【问题描述】:

我有一个列表视图。 ListView 的 Items 保存在一个名为 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");
 }

【问题讨论】:

  • 你能提供一个堆栈跟踪吗?
  • 欢迎来到 Stackoverflow!如果您发现回复有帮助,请投票。如果回复成功回答了您的问题,请点击旁边的绿色复选标记接受答案。也请查看stackoverflow.com/questions/how-to-ask 以获取有关如何写出好问题的建议
  • 尝试启动 Intent 时出现问题 =/ 但我不知道为什么

标签: android listview save instance state


【解决方案1】:

如果您的 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

【讨论】:

  • 不,这不是问题,onClick 事件有效,但只有当我添加 instancestatesave 的代码时才会崩溃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多