再次向上滚动时,Android ArrayAdapter空对象引用错误

时间:2019-03-17 21:38:03

标签: android android-arrayadapter

[更新] 这是指向非常简单的项目的链接,以查看代码 Download Project

我正在使用单选按钮创建自定义适配器。当列表视图显示时,我没有任何错误,但是一旦再次向上滚动以显示第一个隐藏的广播组,就会得到

.grid-container {
    grid-template-columns: 200px 1fr;
    grid-template-areas: 
      "sidenav header"
      "sidenav main";
      box-shadow: 5px 10px !important;
  }

nav {
  grid-area: sidenav;
  box-shadow: 0 -1px 12px 2px whitesmoke;
  background-color: white;
  display: none;
  z-index: 100;
  flex-direction: column;
}

.header {
  grid-area: header;
  background-color: #f8f9ff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
}

main {
  grid-area: main;
  background-color: #f8f9ff;
}

当我为新的单选按钮组中的第一个单选按钮设置文本时

这是列表项

Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference

和适配器

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/q_text"
    android:text="question"
    style="@style/q_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RadioGroup
    android:id="@+id/q_answers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option1"/>

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option2"/>

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option3"/>

</RadioGroup>

<Button
    style="@style/view_answer_btn"
    android:id="@+id/view_answer_btn"
    android:text="@string/view_answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

此行引发错误

public class QuestionsAdapter extends ArrayAdapter<Questions> {

    public QuestionsAdapter(Activity context, ArrayList<Questions> questions) {
        super(context, 0, questions);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        Questions currentQuestion = getItem(position);

        TextView questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
        questionTextView.setText( currentQuestion.getmQuestionTitle());

        RadioGroup radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

        RadioButton firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
        firstAnswer.setText( currentQuestion.getQuestionAnswer1() );
        firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

        RadioButton secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
        secondAnswer.setText( currentQuestion.getQuestionAnswer2() );
        secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));

        RadioButton thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
        thirdAnswer.setText( currentQuestion.getQuestionAnswer3() );
        thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

        return listItemView;
    }
}

为什么我在这里出现错误,这意味着尽管在滚动之前运行活动时列表正确显示,但找不到firstAnswer单选按钮

enter image description here

2 个答案:

答案 0 :(得分:1)

When you scroll listview your radio button will be null with your code. Your project will work when you will fix your project just as below code.

    @NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = 
  LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

        final ViewHolder viewHolder = new ViewHolder();
        try {
            Questions currentQuestion = getItem(position);

             viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
             viewHolder.questionTextView.setText(currentQuestion.getmQuestionTitle());

             viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

            viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
            viewHolder.firstAnswer.setText(currentQuestion.getQuestionAnswer1());
            viewHolder.firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

            viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
            viewHolder.secondAnswer.setText(currentQuestion.getQuestionAnswer2());
            viewHolder.secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));
            viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
            viewHolder.thirdAnswer.setText(currentQuestion.getQuestionAnswer3());
            viewHolder.thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
    viewAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else{
        viewHolder = (ViewHolder) listItemView.getTag();
    }

    return listItemView;
}
private class ViewHolder {
    TextView questionTextView;
    RadioGroup radioGroup;
    RadioButton firstAnswer, secondAnswer,thirdAnswer;
}

答案 1 :(得分:0)

Try grabbing a reference to your RadioButtons like this instead. Notice the difference in the findViewById() calls:

public class QuestionsAdapter extends ArrayAdapter<Questions> {

public QuestionsAdapter(Activity context, ArrayList<Questions> questions) {
    super(context, 0, questions);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    Questions currentQuestion = getItem(position);

    TextView questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
    questionTextView.setText( currentQuestion.getmQuestionTitle());

    RadioGroup radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

    RadioButton firstAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option1);
    firstAnswer.setText( currentQuestion.getQuestionAnswer1() );
    firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

    RadioButton secondAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option2);
    secondAnswer.setText( currentQuestion.getQuestionAnswer2() );
    secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));

    RadioButton thirdAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option3);
    thirdAnswer.setText( currentQuestion.getQuestionAnswer3() );
    thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

    return listItemView;
}

}