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

发布于 2024-12-16 01:26:02 字数 1429 浏览 3 评论 0原文

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

I have a ListView. The Items of the ListView are saved in a ArrayList called MSGs.
Now i implented onSaveInstanceState and onRestoreInstanceState to my class.

The thing with the orientation change works, but when i click on a item of the ListView the App crash.

I dont know what the problem could be.

    @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");
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-12-23 01:26:02

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

If your ListView class is extending the android api ListActivity, the way I handle clicking or selecting one of the list items is through overriding the onListItemClick method, like so:

/**
   * 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文