【发布时间】: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