我已成立了一個AlertDialog
這樣的:AlertDialog getButton()方法返回null
AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this);
noteAlert.setTitle("Title");
noteAlert.setMessage("Message");
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
noteAlert.setNeutralButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
noteAlert.setNegativeButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
AlertDialog alertDialog = noteAlert.create();
Button deleteButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
if (someCondition != 1)
// code runs till here
deleteButton.setEnabled(false); // code works on deleting this line
noteAlert.show();
當我運行上面的代碼,它的工作原理,直到if
聲明。然後該應用程序崩潰(我假設getButton()
拋出一個NPE)。我看到很多其他答案,它們提供了與解決方案相同的代碼來禁用按鈕。
當我註釋掉setEnabled()
行時,該應用程序正常工作(只有按鈕未禁用)。所以基本上我試圖禁用這個NegativeButton,它不工作。你們可以提出一些解決方案嗎?
logcat的:
8月7日至13日:01:14.378:d/ViewRootImpl(19779):的ViewRoot TouchDown的(絕對)DOWN(380,691)
8月7日至13日: 01:14.495:E /對話框(19779):AlertDiablog開始
8月7日至13日:01:14.495:E/hasnote(19779):0
8月7日至13日:01:14.511:E/hasnote (19779):0
8月7日至13日:01:14.511:d/AndroidRuntime(19779):關閉VM
8月7日至13日:01:14.511:W/dalvikvm(19779):線程ID = 1:螺紋與未捕獲離開例外 (組= 0x40e392a0)
8月7日至13日:01:14.519:E/AndroidRuntime(19779):致命異常:主
8月7日至13日:01:14.519:E/AndroidRuntime(19779) :java.lang.NullPointerException
07-13 08:01:14.519:E/AndroidRuntime(19779):at com.exam ple.sherlockcaldroid2.TestSubjectCalendar $ 1 $ 2.onClick(TestSubjectCalendar.java:250)
8月7日至13日:01:14.519:E/AndroidRuntime(19779):在 com.android.internal.app.AlertController $ ButtonHandler。的handleMessage(AlertController.java:1 66)
八月七日至13日:01:14.519:E/AndroidRuntime(19779):在 android.os.Handler.dispatchMessage(Handler.java:99)
07-13 08:01:14.519:E/AndroidRuntime(19779):at android.os.Looper.loop(Looper.java:137)
07-13 08:01:14.519:E/AndroidRuntime(19779):at android.app.ActivityThread.main(ActivityThread.java:4849)
07-13 08:01:14.519:E/AndroidRuntime(19779) ):在 java.lang.reflect.Method.invokeNative(本機方法)
八月7日至13日:01:14.519:E/AndroidRuntime(19779):在 java.lang.reflect.Method.invoke(方法.java:511)
07-13 08:01:14.519:E/AndroidRuntime(19779):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。的java:795)
8月7日至一十三日:01:14.519:E/AndroidRuntime(19779):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07- 13 08:01:14.519:E/AndroidRuntime(19779):at dalvik.system.NativeStart.main(Native Method)
07-13 08:01:34.089:I/Process(19779):發送信號。 PID:19779 SIG:9
你可以發佈什麼logcat說? – Razgriz
@Razgriz我已經添加了logcat –