I am doing an App of test questions and when making the adapter I must have an error because when I run the App it closes automatically. The main activity is the following:
public class ActivityTest extends AppCompatActivity {
private Button back;
private Test test;
private ListView listViewTest;
private TextView textViewQuestion;
private ArrayAdapter<Question> adapter;
private RadioButton[] radioButtonsAnswers;
private int contOK;
private boolean[] Verify;
private String correctAnswer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
test = GeneradorTest.generarTest();
listViewTest = (ListView) findViewById(R.id.listViewtest);
adapter = new TestAdapter(this,R.id.activity_pregunta,R.id.textViewQuestion,R.id.imageViewQuestion,R.id.radiogroup_answers,R.id.check,test.getQuestions());
listViewTest.setAdapter(adapter);
back = (Button)findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
With this the question was generated well and showed the question and the possible answers.
Putting the next adapter is when it gives an error and the App closes:
public class TestAdapter extends ArrayAdapter<Question> {
private int buttonGroup;
private int imageViewQuestion;
private int imageViewVerify;
private int[] radioButtons;
public TestAdapter(Context context, int layout, int textViewQuestion, int imageViewQuestion, int buttonGroup, int imageViewVerify, List<Question> objects) {
super(context, layout, textViewQuestion, objects);
this.buttonGroup = buttonGroup;
this.imageViewQuestion =imageViewQuestion;
this.imageViewVerify = imageViewVerify;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View result = convertView;
RadioGroup rg = (RadioGroup) convertView.findViewById(buttonGroup);
ImageView imviewQuestion = (ImageView) convertView.findViewById(imageViewQuestion);
ImageView imviewVerify = (ImageView) convertView.findViewById(imageViewVerify);
Question q = this.getItem(position);
imviewQuestion.setImageBitmap(q.getImage());
if(q.getResposta()==-1 || !q.isCorrect()) {
imviewVerify.setImageBitmap(null); //imatge a mostrar
}
else{
imviewVerify.setImageBitmap(null); //imatge correcte
}
rg.check(q.getResposta());
for(int i=0; i<q.getRespostes().length; i++) {
((RadioButton)rg.getChildAt(i)).setText(q.getRespostes()[i]);
}
// rg.setOnCheckedChangeListener();//quan canvi el seleccionat, modifica resposta
return super.getView(position, convertView, parent);
}
}
The error he gives me is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
I would be grateful if anyone could know where the error might be.
Another thing that I would like to know is how to implement the setOnCheckedChangeListener () in order to change the selected answer.
Thank you.