ImageView를 ArrayList에 저장하는 논리에 문제가 있습니다.ImageView ArrayList에서 NullPointerException을 발생합니다.
개발중인 응용 프로그램은 게임에서 플레이어 상태를 추적합니다. 사용자는 먼저 Array 객체에 상태 문자열과 상태 이미지를 추적하는 Player 객체를 추가합니다 (모든 객체를 추적하기 위해). 그런 다음 모든 플레이어를 제출하면 버튼 (플레이어의 프로필을 볼 수 있음), ImageView (상태를 나타내는 아이콘) 및 TextView (플레이어의 상태 문자열이 들어있는)가 포함 된 각 플레이어의 TableRow를 부 풀리는 팝업 창이 나타납니다 값).
버튼과 각 플레이어의 프로필로드에 문제가 없습니다. 이 문제는 dialog_select_icon.xml, 특히 ImageView ArrayList에서 "상태 선택"GUI를로드 할 때 발생합니다. NullPointerException을 얻습니다. 왜냐하면 저는 버튼을 한 것과 같은 방식으로하기 때문에 그것을 이해할 수 없습니다. imageID [I] 및 statusID는 [i]는 각각의 상태를 캐릭터 이미지 및 상태에 대한 ID를 포함하는 배열을 int로 언급되는
//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();
}
참고.
xml 파일을 게시 할 수 있지만 길이가 124 줄이므로 길지 않아야합니다. 그냥, XML 파일의 각 ImageView에 ID가있어, "if (! selectStatusIVList.isEmpty())"부분부터 시작하여 모든 NullPointerExceptions를 얻는 이유를 알 수 없습니다. 후에 다른 전화.
도와주세요!
NPE는 일반적으로 찾을 수있는 easiers 오류입니다 : 때문에, 위의 라인을 변경합니다. 귀하의 경우 NPE가 호출에서 시작한다고 말하면 : "if (! selectStatusIVList.isEmpty())",이 가장 likley는 selectStatusIVList를 찾을 수 없다는 것을 의미합니다. 귀하의 코드 읽기 나는이 목록을 시작하는 곳을 볼 수 없습니다. 우리가 볼 수 있도록 코드 조각을 게시하십시오. 아마도 잘못된 이름을 지정했거나 목록이 범위를 벗어났습니다. –
dialog_status_icon.xml의 각 ImageView에는 onClick = "statusIconClicked"속성이 있으므로 아무 것도 호출하지 않습니다. 그냥 ... 거기에있어. 하하. http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html (여기서는 '더 쉬운 클릭 리스너'참조)에서 얻었습니다. –
감사합니다 지미, 당신은 분명히 반만 맞습니다. 인스턴스 변수이지만 onCreate()에서 초기화하는 것을 잊었습니다. 그러나 그렇게 한 후에는 다른 행을 제외하고는 여전히 NPE 오류가 발생합니다. 이제 SelectViewItem을 ArrayList에 추가 할 때 selectStatusIVList.add (ImageView) statusIconGUI.findViewById (R.id.statusIV0));와 같이 시작합니다. –