繁体   English   中英

Android:为什么我的警报对话框中的所有项目都为空?

[英]Android: Why are all items in my alert dialog null?

我正在建立一个包含3个文本字段,2个按钮和1个微调框的警报对话框。 其布局在“ custom_dialog.xml”中。

但是,当它弹出时,所有项目均为空。 我得到一个空指针异常。 如果添加如下所示的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,则弹出警报对话框,并且我看到所有警报框。

编辑1:

Logcat错误(删除了上述if命令);

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)

之所以发生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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM