获取Spinner的所选项而不会导致java.NullPointerException

时间:2017-02-02 23:37:31

标签: java android nullpointerexception

我正在尝试制作一个弹出的AlertDialog并且其中有一个Spinner,其中某人可以选择一个项目,该项目将保存到变量中。但是,当我测试它时,我单击OK按钮,应用程序停止,我在logcat上获得java.NullPointerException。显然,Spinner的getSelectedItem()(应该得到所选项目)导致了这个java.NullPointerException

导致NullPointerException的代码:

 alertDialogBuilder.setView(promptsView);
                    alertDialogBuilder
                            .setCancelable(false)
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {


                                            final Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker);
                                            /*(error is here) -->*/ text = spinner.getSelectedItem().toString(); 



                                            spinner.setAdapter(adapter);
                                            String r = textInput.getText().toString();
                                            allTheTranslatedText = getTranslatedText(r);
                                            textOutput.setText(allTheTranslatedText);
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();


            }
        });

是否有其他方法可以使用检索Spinner的所选项而不会导致NullPointerException?

1 个答案:

答案 0 :(得分:1)

这样做。

您缺少提示View.findViewById();

final Spinner spinner =(Spinner)提示View.findViewById(R.id.LanguagePicker);

View promptsView = LayoutInflator.inflator(......//do it);
alertDialogBuilder.setView(promptsView);
                    alertDialogBuilder
                            .setCancelable(false)
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {

                                            //this is the main line
                                            final Spinner spinner = (Spinner) promptsView.findViewById(R.id.LanguagePicker);
                                            /*(error is here) -->*/ text = spinner.getSelectedItem().toString(); 



                                            spinner.setAdapter(adapter);
                                            String r = textInput.getText().toString();
                                            allTheTranslatedText = getTranslatedText(r);
                                            textOutput.setText(allTheTranslatedText);
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();


            }
        });