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

时间:2017-11-23 12:48:44

标签: android alertdialog android-xml

我想在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>

4 个答案:

答案 0 :(得分:2)

试试这个:

  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

        }
    });

}

答案 1 :(得分:1)

试试这个兄弟!

    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。

答案 2 :(得分:0)

问题在于方法getApplicationContext()。我将其更改为this,现在它的工作一切正常:

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

答案 3 :(得分:0)

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

或试试这个:

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

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