Create spinner with dynamic values

0

I'm trying to create a spinner with dynamic values.

It is giving an error when I try to set the adapter in spinner , below is the error and my code.

java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a
null object reference

Click:

Sqlite sqlite = new Sqlite(getApplicationContext());
            final ArrayList<Ocorrencia> arrayList = sqlite.getListaOcorrencia();
            sqlite.close();

            final Spinner spnocorrencia = view.findViewById(R.id.spnocorrencia);
            OcorrenciaAdapter ocoAdapter = new OcorrenciaAdapter(getApplicationContext(), arrayList);

            Log.i("getItem", String.valueOf(ocoAdapter.getItem(1)));
            spnocorrencia.setAdapter(ocoAdapter); // erro está nessa linha

Adapter:

Context context;
ArrayList<Ocorrencia> arrayList;

public OcorrenciaAdapter(Context context, ArrayList<Ocorrencia> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
}

The error message says that I am trying to set a null value in the adapter, but my adapter returns value in " getCount() " and also in " getItemId() ".

    
asked by anonymous 21.03.2018 / 18:32

1 answer

1

Problem solved!

The problem was at line final Spinner spnocorrencia = view.findViewById(R.id.spnocorrencia);

The view variable did not represent the view my spinner was created.

The creation of my correct view is thus, final View dialogView = inflater.inflate(R.layout.alert_dialog_product ,null); .

Soon I replaced the line with error and left it like this below:

final Spinner spnocorrencia = dialogView.findViewById(R.id.spnocorrencia);

I changed the view variable, by dialogView .

    
21.03.2018 / 19:35