Android 在警报对话框中获取选定微调器项的值
我想在Android 在警报对话框中获取选定微调器项的值,android,android-alertdialog,android-xml,Android,Android Alertdialog,Android Xml,我想在警报对话框中获取所选微调器项。但是如果我试图用Snackbar打印值,我会得到NullPointerException。如何在警报对话框中引用微调器值 例外情况: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedList
警报对话框
中获取所选微调器项。但是如果我试图用Snackbar
打印值,我会得到NullPointerException
。如何在警报对话框中引用微调器值
例外情况:
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
}
});
}
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共无效项(AdapterWebArg0、视图、整数arg2、长arg3){
所选字符串值=微调器按钮。getSelectedItem().toString();
Toast.makeText(getApplicationContext(),已选定),
吐司。长度(短)。show();
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
试试这个:
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
}
});
}
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共无效项(AdapterWebArg0、视图、整数arg2、长arg3){
所选字符串值=微调器按钮。getSelectedItem().toString();
Toast.makeText(getApplicationContext(),已选定),
吐司。长度(短)。show();
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
试试这个兄弟强>
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();
}
});
您也可能会丢失alert.show,因此当您单击Fab按钮时,它不会显示任何内容,因为您从未发送过AlertDialog。试试这个兄弟强>
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();
}
});
此外,您可能会丢失alert.show,因此当您单击Fab按钮时,它不会显示任何内容,因为您从未发送过AlertDialog。问题在于方法getApplicationContext()
。我把它改成了这个
,现在一切正常:
LayoutInflater li = LayoutInflater.from(this);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
问题出在方法getApplicationContext()
上。我把它改成了这个
,现在一切正常:
LayoutInflater li = LayoutInflater.from(this);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
微调器对象为空。这意味着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)
然后将mClickListener设置为微调器对象。您的微调器对象为空。这意味着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设置为微调器对象。请发布comple异常消息。此外,建议您使用google搜索NullPointerException。并告诉我们哪一个指针是空的。请给我们看一下喷丝头的实例instantiation@y4cO你解决了吗…?请发布复杂的异常消息。此外,我们鼓励你在谷歌上搜索NullPointerException。并告诉我们哪一个指针是空的。请给我们看一下喷丝头的实例instantiation@y4cO你解决了吗…?仍然是NullPointerException,而不是我问题的答案仍然是NullPointerException,而不是我问题的答案不起作用,仍然是和以前一样的例外。原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)”。我想我知道这是怎么回事了!我们缺少将微调器添加到视图中的功能,这就是视图不知道微调器的原因。我将getApplicationContext()
更改为this
,现在一切都正常工作,但仍与以前一样异常。原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)”。我想我知道这是怎么回事了!我们缺少将微调器添加到视图中的功能,因此视图不知道微调器。我将getApplicationContext()
更改为this
,现在一切正常