0

我正在构建一个包含3个文本字段,2个按钮和1个微调器下拉框的警报对话框。这个布局在“custom_dialog.xml”中。Android:为什么我的警报对话框中的所有项目均为空?

但是,当它弹出时,所有的项目都是空的。我得到一个空指针异常。如果我添加如下所示的if条件,将显示警告对话框,如果没有,我会在微调框和按钮上得到空指针异常。

这是代码;请注意,长时间点击MainActivity中的当前视图会显示警报。

View promptsView = li.inflate(R.layout.custom_dialog, null); 
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
alert.setView(promptsView); 
alert.setIcon(R.drawable.airside); 
alert.setTitle("Calculate Distance To"); 
final TextView from = new TextView(MainActivity.this); 
from.setText("From: "+item.getTitle1()); 
final TextView to1 = new TextView(MainActivity.this); 
to1.setText("To: "+item.getTitle1()); 
final TextView result = (TextView) dialog.findViewById(R.id.txt3); 
final AlertDialog alertDialog = alert.create(); 
final Button btn1 = (Button) dialog.findViewById(R.id.btn1); 
final Button btn2 = (Button) dialog.findViewById(R.id.btn2); 


ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,airNames); 
Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
if (spinner!=null){ 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner.setAdapter(adapter); 
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 
@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
toLat=items.get(position).getPosition().latitude; 
toLon=items.get(position).getPosition().longitude; 
} 
@Override 
public void onNothingSelected(AdapterView<?> parent) { 

} 
}); 
} 

if (btn1!=null) 
btn1.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
Double dist=calculateDistance(fromLon,fromLat,toLon,toLat); 
result.setText(dist.toString()); 

} 

}); 
if (btn2!=null) 
btn2.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
dialog.dismiss(); 

} 

}); 

alert.show(); 

请检查我是如何在spinner,btn1和btn2中添加if条件的。这意味着每次运行它时都是空的。使用if条件时,弹出警报对话框,并看到所有警报框。

EDIT1:

logcat的误差(与如果上述命令删除);

12-02 14:44:17.305: E/OK(7093): LONGCLICKED 
12-02 14:44:17.335: E/AndroidRuntime(7093): FATAL EXCEPTION: main 
12-02 14:44:17.335: E/AndroidRuntime(7093): Process: com.mapsupport, PID: 7093 
12-02 14:44:17.335: E/AndroidRuntime(7093): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.mapsupport.MainActivity$2.onMapLongClick(MainActivity.java:457) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.android.gms.maps.GoogleMap$9.onMapLongClick(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.android.gms.maps.internal.zzk$zza.onTransact(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at android.os.Binder.transact(Binder.java:380) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.android.gms.maps.internal.bb.a(SourceFile:93) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.gmm6.c.ac.a(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.gmm6.m.bt.f(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.gmm6.m.ak.onLongPress(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.d.g.onLongPress(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.d.h.c(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.google.maps.api.android.lib6.d.i.handleMessage(Unknown Source) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at android.os.Looper.loop(Looper.java:135) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at android.app.ActivityThread.main(ActivityThread.java:5375) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at java.lang.reflect.Method.invoke(Native Method) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at java.lang.reflect.Method.invoke(Method.java:372) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) 
12-02 14:44:17.335: E/AndroidRuntime(7093):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+2

如果您需要一些帮助,请发布您的LogCat。 –

+0

我没有看到'物品'定义在任何地方或任何东西添加到它。 –

+0

项目被添加,其大小为140.问题是微调,btn1,btn2都是空的。我发布了LogCat,请检查。 – Zac1

回答

2

NPE发生,因为微调是在对话框的布局,而不是在活动布局。

地址:

alertDialog.show(); 

这行代码后:

final AlertDialog alertDialog = alert.create(); 

然后初始化所有的意见后,你叫上你的alertDialog的show()方法和不是之前它:

TextView result = (TextView) promptsView.findViewById(R.id.txt3); 
Spinner spinner = (Spinner) promptsView.findViewById(R.id.spinner1); 
final Button btn1 = (Button) promptsView.findViewById(R.id.btn1); 
final Button btn2 = (Button) promptsView.findViewById(R.id.btn2); 

此外请确保从您的代码末尾删除此行:

alert.show(); 
+0

我改变它,仍然得到相同的错误:“12-02 14:55:46.120:E/AndroidRuntime(9267):致命例外:主 12-02 14:55:46.120:E/AndroidRuntime(9267):进程:com.mapsupport,PID:9267 12-02 14:55:46.120:E/AndroidRuntime(9267):java.lang.NullPointerException:试图调用虚拟方法'void android.widget.Spinner.setAdapter(android.widget。 SpinnerAdapter)'空对象引用 “ – Zac1

+0

请尝试我更新的答案。 –

+0

它的工作,谢谢。是的,我在创建方法后移动了我的alert.show(),并正确初始化了我的文字浏览。对不起,我忘了引用他们来自XML的textviews! – Zac1

相关问题