尝试构建一种结果跟踪器,我遇到了这个问题;
我有一个自定义列表视图(带有标题),每个玩家都有一行。在这一行中,有一个 TextView 用于名称,一个 EditText 用于抛出次数(以及其他)。
在模拟器上运行时,在所有情况下都运行良好。
在手机上运行并且不更改任何值时,一切运行正常。
在手机上运行并在 ListView 的最后一行报告值时,代码会跳过第一项,因此在尝试查找最后一行时会出现空指针异常。
报告结果是通过这种方法完成的:
private void reportResults(ListView listViewPlayers, ArrayList<HoleResultInfo> holeResult_data, TextView tvPar, Integer hole){
//Skapar arrya ifrån antalet spelare i tabellen, justerar för header
System.out.println("gui hol Collecting hole results");
System.out.println("gui hol number of items in listview: " + listViewPlayers.getCount());
HoleResultInfo tempHoleResult;
for (int i = 0; i < listViewPlayers.getCount()-1; i++) {
tempHoleResult = holeResult_data.get(i);
//Starts at 1 to skip header
View rowView = listViewPlayers.getChildAt(i+1);
System.out.println("gui hol At i: " + i + " looking for child: " + (i + 1));
System.out.println("gui hol HoleResult name: " + holeResult_data.get(i).getName());
TextView tvName = (TextView)rowView.findViewById(R.id.tvPlayerName);
System.out.println("gui hol Textview name: " + tvName.getText().toString());
tempHoleResult.setName(tvName.getText().toString());
EditText etOb = (EditText)rowView.findViewById(R.id.etPlayerOb);
tempHoleResult.setOb(Integer.parseInt(etOb.getText().toString()));
EditText etTrows = (EditText)rowView.findViewById(R.id.etPlayerTrows);
tempHoleResult.setTrows(Integer.parseInt(etTrows.getText().toString()));
holeResult_data.set(i, tempHoleResult);
}
logcat 的输出(有三个玩家:Otto、Teddy 和 Test)在模拟器中如下所示,并且没有报告:
gui hol Collecting hole results
gui hol number of items in listview: 4
gui hol At i: 0 looking for child: 1
gui hol HoleResult name: Otto
gui hol Textview name: Otto
gui hol At i: 1 looking for child: 2
gui hol HoleResult name: Teddy
gui hol Textview name: Teddy
gui hol At i: 2 looking for child: 3
gui hol HoleResult name: Test
gui hol Textview name: Test
这是不工作时的输出:
gui hol Collecting hole results
gui hol number of items in listview: 4
gui hol At i: 0 looking for child: 1
gui hol HoleResult name: Otto
gui hol Textview name: Teddy
gui hol At i: 1 looking for child: 2
gui hol HoleResult name: Teddy
gui hol Textview name: Test
gui hol At i: 2 looking for child: 3
gui hol HoleResult name: Test
D/AndroidRuntime(15847): Shutting down VM
W/dalvikvm(15847): threadid=1: thread exiting with uncaught exception (group=0x41541318)
E/AndroidRuntime(15847): FATAL EXCEPTION: main
E/AndroidRuntime(15847): java.lang.NullPointerException
at frisbeegolf.gui.Hole.reportResults(Hole.java:141)
at frisbeegolf.gui.Hole.access$0(Hole.java:129)
at frisbeegolf.gui.Hole$1.onClick(Hole.java:69)
at android.view.View.performClick(View.java:4103)
at android.view.View$PerformClick.run(View.java:17117)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
第 141 行是:TextView tvName = (TextView)rowView.findViewById(R.id.tvPlayerName);
请帮忙,我完全没有想法并且为此发疯!
/O