App adapter failed to ask test questions

0

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.

    
asked by FranEET 15.01.2017 в 19:40
source

2 answers

2

The error indicates:

  

java.lang.NullPointerException: Attempt to invoke virtual method   'android.view.View android.view.View.findViewById (int)' on a null   object reference

Which means that you try to call the findViewById(int) method in a view with null value, the error occurs when trying to get a reference of an element in a view but this view, convertView has a null value :

RadioGroup rg = (RadioGroup) convertView.findViewById(buttonGroup);
ImageView imviewQuestion = (ImageView) convertView.findViewById(imageViewQuestion);
ImageView imviewVerify = (ImageView) convertView.findViewById(imageViewVerify);

What is usually done within the method getView () , is to check if the container convertView contains value, if not you have to inflate the view that contains the elements that you would look for inside it:

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


   if( convertView == null ){
    //Creamos la vista:
       convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }

      //  View result = convertView;
        RadioGroup rg = (RadioGroup) convertView.findViewById(buttonGroup);
        ImageView imviewQuestion = (ImageView) convertView.findViewById(imageViewQuestion);
        ImageView imviewVerify = (ImageView) convertView.findViewById(imageViewVerify);

You can see some examples of Adapter and review what is done within getView () :

ListView and Custom Adapter.

Custom Adapter on Android.

    
answered by 16.01.2017 в 19:51
1

You need to inflate your view before you can access it, look at this example:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   User user = getItem(position);    
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
   }
   // Lookup view for data population
   TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
   TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
   // Populate the data into the template view using the data object
   tvName.setText(user.name);
   tvHome.setText(user.hometown);
   // Return the completed view to render on screen
   return convertView;
}
    
answered by 15.01.2017 в 20:29