0

次の問題があります。ListViewテストを表示する があります。各行ListViewには、質問とRadioGroup回答付きの があります。私が経験している問題は、ListView下にスクロールするたびにこのエラーが発生し、そのような動作の原因がわからないことです:

LogCat:

02-05 11:22:56.712  27296-27296/com.fragon.testjson E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.fragon.testjson, PID: 27296
    java.lang.NullPointerException
            at com.fragon.testjson.ListAdapter.getView(ListAdapter.java:38)
            at android.widget.AbsListView.obtainView(AbsListView.java:2255)
            at android.widget.ListView.makeAndAddView(ListView.java:1790)
            at android.widget.ListView.fillDown(ListView.java:691)
            at android.widget.ListView.fillGap(ListView.java:655)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5143)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3243)
            at android.widget.AbsListView.onTouchMove(AbsListView.java:3587)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3431)
            at android.view.View.dispatchTouchEvent(View.java:7837)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2075)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1522)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2023)
            at android.view.View.dispatchPointerEvent(View.java:8017)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3966)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3845)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3405)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3455)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3424)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3531)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3432)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3588)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3405)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3455)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3424)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3432)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3405)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5554)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5534)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5505)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5634)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5607)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5653)
            at andr

マイリストアダプター:

public class ListAdapter extends ArrayAdapter<Question> {


    public ListAdapter(Context context, ArrayList<Question> questions) {
        super(context, R.layout.list_item, questions);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }
        final Question question = getItem(position);
        TextView id = (TextView) convertView.findViewById(R.id.question_id);
        TextView text = (TextView) convertView.findViewById(R.id.question_text);
        id.setText(Integer.toString(question.getId()));
        text.setText(question.getName());

        RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.question);
        rg.removeAllViews();//Line 38. Whithout his line I also get the same error.
        rg.setId(position);
        int i = 0;
        for (String qA : question.answers) {
            RadioButton rb = new RadioButton(getContext());
            rb.setText(qA.toString());
            rg.addView(rb);
            rb.setId(i);
            i++;
        }

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            question.setUserAnswer(checkedId);
            Log.d("User answer", Integer.toString(checkedId));
        }
    });

    return convertView;
}

}

4

2 に答える 2

1

この行rg.setId(position);RadioGroupの ID を 以外のものに変更しているR.id.questionため、Viewリサイクルされたとき (つまり、スクロールしたときfindViewById(R.id.question)) に null が返されます。

位置をキャッシュする必要がある場合は、代わりに メソッドsetTag()getTag()メソッドを使用できます。Question代わりにオブジェクトをキャッシュすることもできます。例えば:

//rg.setId(position);
rg.setTag(question);
...
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        Question q = (Question) group.getTag();
        q.setUserAnswer(checkedId);
        Log.d("User answer", Integer.toString(checkedId));
    }
});

RadioGroup.OnCheckedChangeListenerグループごとに新しいものを作成する代わりに、1 つだけ定義することもできます。

//rg.setId(position);
rg.setTag(question);
...
rg.setOnCheckedChangeListener(checkListener);
...
...
private RadioGroup.OnCheckedChangeListener checkListener =
    new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            Question q = (Question) group.getTag();
            q.setUserAnswer(checkedId);
            Log.d("User answer", Integer.toString(checkedId));
        }
    };
于 2015-02-05T10:34:53.323 に答える
0

RadioGroup の ID を変更します。rg.setId(position);

下にスクロールすると、ListView はビューを再利用します。ID を変更したため、findViewById()が返されnullます。

解決策:

  1. OnCheckedChangeListenerでの位置を認識しているカスタムを実装しListViewます。

    private static class QuestionCheckedListener implements RadioGroup.OnCheckedChangeListener{
    
    private final int mPosition;
    
    QuestionCheckedListener(int position){
        mPosition = position;
    }
    
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        question.setUserAnswer(mPosition);
        Log.d("User answer", Integer.toString(checkedId));
    }
    

    }

  2. でインスタンス化しgetView()ます:

    rg.setOnCheckedChangeListener(new QuestionCheckedListener(position));
    
  3. setId(i)そして、すべての通話を削除することを忘れないでください

于 2015-02-05T10:36:42.883 に答える