2012-03-27 65 views
1

我在將ImageView存儲到ArrayList後面的邏輯遇到了一些麻煩。ImageView ArrayList給出空指針異常

我正在開發的應用程序會跟蹤遊戲中的玩家狀態。用戶首先將一個Player對象(它跟蹤一個狀態字符串和一個狀態圖像跟蹤它)添加到一個ArrayList(以便跟蹤它們全部)。然後,在提交所有玩家後,會彈出一個屏幕,爲每個玩家提供一個TableRow,包含一個按鈕(用於查看玩家的個人資料),一個ImageView(代表狀態的圖標)和一個TextView(包含玩家的狀態字符串值)。

我沒有按鈕和加載每個球員的配置文件的問題。加載來自dialog_select_icon.xml的「選擇狀態」GUI,特別是ImageView ArrayList時會出現問題。我得到一個NullPointerException,這對我來說沒有任何意義,因爲我的操作方式基本上與按鈕的方式相同。

//this code runs when user clicks a player's status icon 
public void playerStatusIconClicked(View v) 
{ 
    //loop through buttons to determine which player's button was clicked 
    for (int i = 0; i < playerList.size(); i++) 
    { 
     if (v.getId() == playerStatusIVList.get(i).getId()) 
     { 
      calledPlayer = i; //instance variable 
      loadStatusIconGUI(); 
     }//if 
    }//for 
}//method playerStatusIconClicked 


//showStatusIconGUI inflates the "select status icon" GUI 
//and handles the user selecting an icon 
private void loadStatusIconGUI() 
{  
    //inflate the GUI for the showStatusIcon dialog (inflater is an instance variable) 
    View view = inflater.inflate(R.layout.dialog_select_icon, null); 
    //if the list has something in it, start from fresh 
    if (!selectStatusIVList.isEmpty()) 
    { 
     selectStatusIVList.clear();   
    } 

    //list of icons in the "select status icon" dialog 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV1)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV2)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV3)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV4)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV5)); 
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV6)); 

    //create a dialog so user can select an icon 
    AlertDialog.Builder selectIconDialog = new AlertDialog.Builder(this); 
    selectIconDialog.setView(view); //set the Dialog's custom view 
    selectIconDialog.setTitle(R.string.title_select_icon); 

    selectIconDialog.setNegativeButton(R.string.close, null); 
    selectIconDialog.show(); 
}//showStatusIconGUI 


//Handle clicks in the "select status icon" dialog 
//Assigns a new status to the player 
public void statusIconClicked(View v) 
{ 
    Toast message; 

    for (int i = 0; i < selectStatusIVList.size(); i++) 
    { 
     if (v.getId() == selectStatusIVList.get(i).getId()) 
     { 
      message = Toast.makeText(
       MafiaTracker.this, "new status: " statusID[i], Toast.LENGTH_SHORT); 
      message.show(); 
      playerList.get(calledPlayer).setImage(imageID[i]); 
      playerList.get(calledPlayer).setStatus(statusID[i]); 
     } 
    } 

    updateViewPlayerGUI(); 
} 

注意,圖像標識[i]和statusID [I]指的是int包含用於每個狀態字符串和狀態的圖像的ID陣列。

我可以發佈xml文件,但由於它是124行,我寧願不要。只要知道xml文件中的每個ImageView都有一個ID,所以我不知道爲什麼我會從「if(!selectStatusIVList.isEmpty())」部分開始獲取這些NullPointerException,然後繼續其他呼叫後。

請幫忙!

+0

NPE通常是easiers錯誤找到,因爲它只是告訴你,你正在嘗試調用的東西不存在。在你的情況下,你說NPE從調用開始:「if(!selectStatusIVList.isEmpty())」,這最有可能意味着它找不到selectStatusIVList。讀取您的代碼我看不到您啓動此列表的位置。張貼這段代碼,以便我們可以看看。可能是您命名錯誤,或列表超出範圍 – 2012-03-27 09:12:19

+0

dialog_status_icon.xml中的每個ImageView都具有onClick =「statusIconClicked」屬性,因此它不會被任何東西調用。只是...在那裏。哈哈。我從這裏獲得:http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html(請參閱「輕鬆點擊監聽器」)。 – 2012-03-27 09:13:37

+0

謝謝吉米,你絕對是對的。這是一個實例變量,但我忘了在onCreate()中啓動它。但是,在這樣做之後,我仍然會得到NPE錯誤,除了不同的行。現在它發生在我將Im​​ageViews添加到ArrayList時,從以下位置開始:selectStatusIVList.add((ImageView)statusIconGUI.findViewById(R.id.statusIV0));. – 2012-03-27 09:19:17

回答

1

statusIconGUI似乎是你setContenView(). 使用可考慮行的主要佈局的xml:

selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0)); 
您在使用statusIconGUI findViewbyID

。這樣做,而不是你膨脹的R.layout.dialog_select_icon的視圖實例。 因此,改變上述行:

selectStatusIVList.add((ImageView) view.findViewById(R.id.statusIV0)); 
+1

非常感謝,這絕對是。我改變了它,所以LayoutInflater是一個實例變量而不是本地的,並且在改變之前我一定完全錯過了它。真棒! – 2012-03-27 09:30:12

+0

歡迎。如果您發現有用的答案,請立即投票。 :) – Akhil 2012-03-27 09:33:34

+0

我會如果我能。 (顯然你需要15的聲望......我還是新的,呃。) – 2012-03-27 12:23:07

1

最初selectStatusIVList爲null。在loadStatusIconGUI檢查其空

if(selectStatusIVList != null){ 
    if (!selectStatusIVList.isEmpty()) 
    { 
     selectStatusIVList.clear();   
    } 
}else{ 

    selectStatusIVList = new ArrrayList<Integer>(); 
}