Spinner.setadapter on a null Object Reference

Asked

Viewed 459 times

0

The array of players is filled with the dice but gives error in the method setadapter.

Spinner spinnerJogador1;

   ArrayList<JogadorModel> jogadores = jogadorRepository.SelecionarDisponiveis();

dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, jogadores);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
        // attaching data adapter to spinner
        spinnerJogador1.setAdapter(dataAdapter);
  • 1

    Hello, probably your spinner is null, please can post the error log ?

  • @Ultraseven put the Spinner code for easy help! D

  • 07-04 06:39:39.512 4198 4198 E Androidruntime FATAL EXCEPTION: main 07-04 06:39:39.512 4198 4198 E Androidruntime Process: com.mycompany.Marcatimevolei, PID: 4198 java.lang.Runtimeexception: Unable to start activity Log do erro: ComponentInfo{com.mycompany.MarcaTimeVolei/com.mycompany.MarcaTimeVolei.MontarDuplaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null Object Reference

  • Hello @Arthurcordovastapassoli, the error log is in the previous comment.

  • Hello @mauriciocaserta. The code is complete, I have not yet entered the code to capture the user’s choice.

  • @Ultraseven referred to the code referring to the "spinnerJogador1", could paste the code in which it assigns value to this variable?

  • @Ultraseven verified in debug whether the variable spinnerJogador1 is null ?

  • I entered the spinner declaration in the question.

  • @Rthur spinner is null.

Show 4 more comments

1 answer

1


@Ultrasenven vc need to assign the spinnerGood1 the Spinner component you put in the view, so try:

sua_view.xml

<Spinner
    android:id="@+id/id_do_seu_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Suaactivity.java

Spinner spinnerJogador1 = (Spinner) findViewById(R.id.id_do_seu_spinner);
  • It worked! Thank you very much. I’m now searching how to capture the selected player id.

  • Cool, if you use a list can do inside his Adpter. Hug, if you can score the answer I thank you, hug!

  • Thank you. I use Adapter based on examples, but I confess that this subject is still very confusing for me, and unfortunately not only this but several others.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.