1

我在将 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())”部分开始,然后继续每个之后的其他电话。

请帮忙!

4

2 回答 2

1

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

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

     selectStatusIVList = new ArrrayList<Integer>();
}
于 2012-03-27T09:14:17.480 回答
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));
于 2012-03-27T09:25:25.863 回答