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

相關問題