Error creating a spinner in Android Studio

1

When trying to create a Spinner with its adapter and data already created it gives me an error of NullPointerException , and I do not know where it is given that everything is created.

public class PartidaActivity extends AppCompatActivity {

private Button bAyuda;
private Button bTirar;
private ImageView dado;
private ImageView dado2;
private SoundPool sp;
private int sonidoDado=0;
private TextView nombreJugador;
private int turno;
private String valor,valorRecibido;
private Spinner spinnerValores;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pantalla_partida);

    bAyuda = (Button) findViewById(R.id.botonAyudaPartida);
    bTirar = (Button) findViewById(R.id.botonTirar);
    dado = (ImageView) findViewById(R.id.Dado);
    dado2 = (ImageView) findViewById(R.id.Dado2);
    nombreJugador = (TextView) findViewById(R.id.nombreJugador);
    spinnerValores = (Spinner) findViewById(R.id.eleccionTirada);

    ArrayList<Valor> listaValores = new ArrayList<Valor>();

    listaValores.add(new Valor(1,"Kiriki"));
    listaValores.add(new Valor(2,"Ladrillo"));
    listaValores.add(new Valor(3,"Pareja"));
    listaValores.add(new Valor(4,"10"));
    listaValores.add(new Valor(5,"9"));
    listaValores.add(new Valor(6,"7"));
    listaValores.add(new Valor(7,"6"));
    listaValores.add(new Valor(8,"5"));
    listaValores.add(new Valor(9,"4"));

    ArrayAdapter<Valor> adaptador = new ArrayAdapter<Valor>(this,android.R.layout.simple_spinner_item,listaValores);
    adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerValores.setAdapter(adaptador);

I now put what the error tells me.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.alex.proyecto, PID: 16525
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.proyecto/com.example.alex.proyecto.PartidaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6938)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
                  at com.example.alex.proyecto.PartidaActivity.onCreate(PartidaActivity.java:66)
                  at android.app.Activity.performCreate(Activity.java:7174)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6938) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
    
asked by Alejandro Moreno 10.06.2018 в 22:04
source

1 answer

0

You are saying that the Spinner reference is null. You have to confirm if the Spinner in xml has the same id that you pass as a parameter to the findViewById() method. Which in this case is eleccionTirada . Confirm in xml if the Spinner has that same id.

Or you have to confirm that the Spinner is in the layout that you are inflating with the setContentView(); method. The layout you are inflating is pantalla_partida , make sure that this is the layout that contains the Spinner.

If all is well and the problem persists as a Clean Project . This option is found in the Build option on the Android Studio toolbar.

    
answered by 11.06.2018 / 00:10
source