2014-09-05 82 views
0

當我向主活動發送對象時,我的應用程序總是崩潰。我需要它,所以主要活動顯示我的班級的字符串,但每次我按第二個活動中的行並返回到主要活動的應用程序崩潰可以有人請幫助我解決此問題,謝謝。將對象發送給其他活動時出錯

我的課以下

public class Ship implements Serializable { 


public String test() 
{ 
    String s = "Hello Android!"; 
    return s; 
} 

}

次活動

hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 


      Intent sendListEvents = new Intent(); 
      if(position == 1) 
      { 

       Ship test = new Ship(); 
       sendListEvents.putExtra("ship1", test); 

      } 
      //insert position events ^^^ 
      setResult(RESULT_OK, sendListEvents); 


} 
    }); 

主要活動如下

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == result) { 

     if (resultCode == RESULT_OK) { 

      Intent getData = getIntent(); 
      Ship ship1 = (Ship) getData.getSerializableExtra("ship1"); 
      String s = ship1.test(); 
      test.setText(s); 

     } 
    } 

}

+0

你點擊了哪一行?只有當你點擊第二行時,你的意圖纔會包含額外的內容。此外,你把「ship1」並獲得「Ship1」。感覺不同 – 2014-09-05 23:20:26

+1

從['LogCat'](http://developer.android.com/tools/help/logcat.html)提供堆棧跟蹤。 – 2014-09-05 23:21:30

+0

@AlexanderZhak我將Ship1更改爲ship1,但仍然不起作用 – XetronProgrammer 2014-09-05 23:46:11

回答

0

嘗試更換此:

Intent getData = getIntent(); 
Ship ship1 = (Ship) getData.getSerializableExtra("Ship1"); 

有了這個:

Ship ship1 = (Ship) data.getSerializableExtra("ship1"); 

而且,我想,當建議在做 「SHIP1」 最後的靜態常量的值,以避免錯別字參考它。如果您需要知道列表中被點擊的位置,您可以將其作爲另一個意圖額外傳遞。

相關問題