2014-09-23 46 views
0

我有一個字符串數組,並且有一個名爲ship1的對象,它位於onItemClickListener中的if語句中。我想要一個對象值(addSpend)顯示在數組中的一個字符串中,但由於某種原因字符串數組無法識別或查看ship1。我如何解決這個問題? (我想旁邊顯示的「遊俠船」附加支出值)HangarActivity線28字符串數組不能識別對象的值和對象本身

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.hangar); 

    String[] ship = {"Scout Ship", "Ranger Ship" , "Gun Ship MK.1", 
      "Conquerer Ship", "Gun Ship MK.2", "Tank Ship", 
      "Battle Ship MK.1", "Titan Ship", "Battle Ship MK.2", 
      "Colossal Titan Ship"}; 

    ListView hangarList = (ListView) findViewById(R.id.hangarList); 
    ListAdapter adapter = new HangarAdapter(this, ship); 

hangarList.setAdapter(adapter); 

hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() 

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

    Intent sendListEvents = new Intent(); 
    if (position == 0) { 
     Ship ship1 = new Ship(); 
     ship1.setAddAmount(50); 
     ship1.setAddTime(5000); 
     ship1.setAddSpend(1000); 
     ship1.shipAdd(); 
    } 
+0

你的二傳手在哪裏? – 2014-09-23 19:02:16

+1

好心發佈錯誤消息,使調試變得容易。 – User27854 2014-09-23 19:02:39

+0

setContentView(R.layout.hangar)'<==這裏的半列 – Kiloreux 2014-09-23 19:03:33

回答

0

基於堆棧跟蹤代碼,你提領一空指針,這是在構造函數中。看看這個構造函數,並找出爲什麼該行上的對象被解除引用爲null,以及如何解決這個問題。

附註:您在原始問題中粘貼的代碼看起來與問題完全無關,並且實際問題顯示在異常的堆棧跟蹤中。當@ user2900314試圖讓你粘貼堆棧跟蹤時,這是一個很好的理由。