ImageView ArrayList给出NullPointerException

时间:2012-03-27 09:05:41

标签: android arraylist imageview layout-inflater

我在将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(); 
}

请注意,imageID [i]和statusID [i]指的是包含每个状态字符串和状态图像的ID的int数组。

我可以发布xml文件,但由于它是124行,我不愿意。只知道xml文件中的每个ImageView都有一个ID,所以我无法弄清楚为什么我会得到这些NullPointerExceptions,从“if(!selectStatusIVList.isEmpty())”部分开始,然后继续其他电话之后。

请帮忙!

2 个答案:

答案 0 :(得分:1)

最初,selectStatusIVList为null。在loadStatusIconGUI中,检查它是否为null

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

     selectStatusIVList = new ArrrayList<Integer>();
}

答案 1 :(得分: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));