在警报对话框中获取所选 Spinner 项目的值

Getting the value of selected Spinner item in an alert dialog

我想在 AlertDialog 中获取选定的微调器项目。但是,如果我尝试使用 Snackbar 打印值,我会得到 NullPointerException。如何引用 AlertDialog 中的微调器值?

异常:

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

对话框的代码:

    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>

试试这个:

  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

        }
    });

}

试试这个兄弟!

    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();
                        }
                    });

如果可行,请告诉我,如果不行,我会继续尝试,直到找到解决方案! ;)

P.S 我更新了我的答案,但我不完全确定。看看下一个例子:

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();

您也可能会遗漏 alert.show ,因此当您单击 Fab 按钮时它不会执行任何操作,因为您从未发送过 AlertDialog。

问题出在方法 getApplicationContext() 上。我将其更改为 this,现在一切正常:

    LayoutInflater li = LayoutInflater.from(this);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

您的微调器对象为空。这意味着 Spinner spinner = (Spinner)findViewById(R.Id.spinner) 正在返回空对象。交叉检查 contentView 的布局。

或者试试这个:

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

然后将 OnItemClickListener 设置为微调器对象。