0

I wanted to get the selected spinner item in an AlertDialog. But if I'm trying to print the value with Snackbar I'm getting a NullPointerException. How can I refer to the spinner value in the AlertDialog?

The Exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference.

Dialog's code:

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog alert = builder.create();

    final Spinner spinner = (Spinner)findViewById(R.id.spinner);

    // Set title of the dialog
    builder.setTitle("ToDo hinzufügen");
    // Set view to dialog.xml
    builder.setView(R.layout.dialog);


    // To add ToDos for a specific section
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/

            // save entry in database and refresh the page
            builder.setPositiveButton("Hinzufügen", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alert.dismiss();


                    Snackbar.make(findViewById(R.id.fab), spinner.getSelectedItem().toString(), Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:hint="Kategorie auswählen"
        android:entries="@array/tab_categeory"
        android:spinnerMode="dialog"
        />

    <EditText
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Was möchten Sie erledigen?"/>
</LinearLayout>
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
y4cO
  • 55
  • 12

4 Answers4

2

Try this :

  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?>arg0, View view, int arg2, long arg3) {

            String selected_val=spinner_button.getSelectedItem().toString();

            Toast.makeText(getApplicationContext(), selected_val ,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}
Fadwa_lmh
  • 276
  • 2
  • 11
1

Try this bro!

    private String value;
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            final Spinner spinner = (Spinner)findViewById(R.id.spinner);

            // Set title of the dialog
            builder.setTitle("ToDo hinzufügen");
            // Set view to dialog.xml
            builder.setView(R.layout.dialog);

            spinner .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                    value= adapterView.getItemAtPosition(position).toString;

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });

 final AlertDialog alert = builder.create();

            // To add ToDos for a specific section
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();*/

                    // save entry in database and refresh the page
                    builder.setPositiveButton("Hinzufügen", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alert.dismiss();


                            Snackbar.make(findViewById(R.id.fab), value, Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                        }
                    });

Let me know if this works, if not , Im here to keep trying until we get the solution! ;)

P.S I updated my answer but I am not completely sure. Take a Look to the next example:

LayoutInflater li = LayoutInflater.from(getApplicationContext()); //or context() if depends

View promptsView = li.inflate(R.layout.my_dialog_layout, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setView(promptsView);

// set dialog message

alertDialogBuilder.setTitle("My Dialog..");
alertDialogBuilder.setIcon(R.drawable.ic_launcher);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();

final Spinner mSpinner= (Spinner) promptsView
        .findViewById(R.id.mySpinner);
final Button mButton = (Button) promptsView
        .findViewById(R.id.myButton);

// reference UI elements from my_dialog_layout in similar fashion

mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

// show it
alertDialog.show();

Also you can be missing alert.show , so when you clicked the Fab button it does not anything because you have never sent the AlertDialog.

Cristofer
  • 1,046
  • 13
  • 21
  • Not working, still the same exception as before. **Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference**. Line: spinner.setOnItemSelectedListener – y4cO Nov 23 '17 at 19:23
  • I think I know what is wrong with this! We are missing to add our Spinner to our view, so that is why the view does not know the Spinner. – Cristofer Nov 23 '17 at 21:06
  • i changed `getApplicationContext()` to `this` and now everything works well – y4cO Nov 24 '17 at 21:00
0

The problem was with the method getApplicationContext(). I changed it to this and now its working all good:

    LayoutInflater li = LayoutInflater.from(this);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
y4cO
  • 55
  • 12
0

Your spinner object is null. It means Spinner spinner = (Spinner)findViewById(R.Id.spinner) is returning null object. Cross check the layout of the contentView.

Or try this:

View v = inflater.inflate(R.layout.dialog, null); // this line          
 builder.setView(v);
 Spinner spinner = (Spinner)v.findViewById(R.id.spinner)

Then set OnItemClickListener to the spinner object.

Y_Y
  • 331
  • 1
  • 6