【问题标题】:Getting the value of selected Spinner item in an alert dialog在警报对话框中获取选定 Spinner 项的值
【发布时间】:2017-11-23 12:48:44
【问题描述】:

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

【问题讨论】:

  • 请发布完整的异常消息。
  • 进一步鼓励您使用谷歌搜索 NullPointerException。并告诉我们哪个指针为空。
  • 请向我们展示微调器的实例化
  • 我添加了实例化
  • @y4cO 你解决了吗...?

标签: android android-alertdialog android-xml


【解决方案1】:

试试这个:

  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

        }
    });

}

【讨论】:

  • 仍然是 NullPointerException 而不是我的问题的答案
【解决方案2】:

试试这个兄弟!

    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。

【讨论】:

  • 不工作,还是和以前一样的异常。 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)”。行:spinner.setOnItemSelectedListener
  • 我想我知道这有什么问题!我们缺少将 Spinner 添加到我们的视图中,这就是视图不知道 Spinner 的原因。
  • 我将getApplicationContext() 更改为this,现在一切正常
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    您的微调器对象为空。这意味着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 设置为微调器对象。

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 2012-01-01
      • 2012-08-29
      • 2020-05-16
      • 2017-03-09
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多