2011-11-16 89 views
1

我有一個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"); 
} 
+1

你可以提供一個堆棧跟蹤? –

+0

歡迎來到Stackoverflow!如果您發現回覆有幫助,請投票。如果回覆成功回答您的問題,請點擊旁邊的綠色複選標記以接受答案。也請看看http://stackoverflow.com/questions/how-to-ask關於如何寫一個好問題的建議 –

+0

當它嘗試啓動意圖= /但是我不知道爲什麼 – Stefan

回答

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 
+0

這個問題發生不。問題,onClick事件的作品,但只有當我添加實例的代碼時,它會崩潰 – Stefan

相關問題