Android 再次向上滚动时出现 ArrayAdapter 空对象引用错误
Android ArrayAdapter null object reference error while scrolling up again
[更新]
这是非常简单的项目查看代码的link
Download Project
我正在创建一个带有单选按钮的自定义适配器。当显示列表视图时,我没有收到任何错误,但是一旦我再次向上滚动以显示第一个隐藏的无线电组,我得到
Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference
当我为新单选组中的第一个单选按钮设置文本时
这是list_item
<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.setText( currentQuestion.getQuestionAnswer1() );
为什么我在这里得到错误,这意味着它找不到 firstAnswer 单选按钮,虽然列表在 运行 activity 滚动
之前正确显示
当您滚动列表视图时,您的单选按钮在您的代码中将为空。当您按照以下代码修复您的项目时,您的项目将正常运行。
@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;
}
改为像这样获取对 RadioButton 的引用。注意 findViewById()
调用的区别:
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;
}
}
[更新] 这是非常简单的项目查看代码的link Download Project
我正在创建一个带有单选按钮的自定义适配器。当显示列表视图时,我没有收到任何错误,但是一旦我再次向上滚动以显示第一个隐藏的无线电组,我得到
Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference
当我为新单选组中的第一个单选按钮设置文本时
这是list_item
<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.setText( currentQuestion.getQuestionAnswer1() );
为什么我在这里得到错误,这意味着它找不到 firstAnswer 单选按钮,虽然列表在 运行 activity 滚动
之前正确显示当您滚动列表视图时,您的单选按钮在您的代码中将为空。当您按照以下代码修复您的项目时,您的项目将正常运行。
@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;
}
改为像这样获取对 RadioButton 的引用。注意 findViewById()
调用的区别:
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;
}
}