0

この問題に遭遇したタイプの結果トラッカーを構築しようとしています。

各プレイヤーが行を取得する 1 つのカスタム リストビュー (ヘッダー付き) があります。この行には、名前用の TextView と、スロー数用の 1 つの EditText があります (特に)。

エミュレーターで実行すると、すべての場合に問題なく動作します。

電話で実行し、値を変更しない場合、すべて正常に実行されます。

電話で実行し、ListView の最後の行の値を報告すると、コードは最初の項目をスキップするため、最後の行を見つけようとすると null ポインター例外が発生します。

結果の報告は、次の方法で行われます。

    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 の 3 人のプレーヤー) からの出力は、エミュレーターでは次のようになり、報告されません。

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

4

1 に答える 1

0

メソッド getChildAt() は可視ビューを取得します

View rowView = listViewPlayers.getChildAt(i+1); 

ArrayList などのデータ リストのデータを変更し、リスト アダプターから notifydataSetChange メソッドを呼び出して ListView に通知する必要があります。

adapter.notifyDataSetChange();

アダプターは、メソッド getView を呼び出して、表示されているセルのすべてのデータを変更する必要がありますが、質問には添付されていません

于 2013-06-07T08:39:47.330 に答える