滚动包含radiogroups的listview时的NPE

时间:2015-02-05 10:28:18

标签: android android-listview android-radiogroup

我有以下问题。我有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

我的ListAdapter:

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;
}

}

2 个答案:

答案 0 :(得分:1)

rg.setId(position);正在将RadioGroup的ID更改为R.id.question以外的其他ID,因此当View被回收时 - 即滚动时findViewById(R.id.question) 1}}返回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,而不是为每个组创建一个新的。{/ p>

//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));
        }
    };

答案 1 :(得分: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)来电