2013-07-13 204 views
1

我已成立了一個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

+0

你可以發佈什麼logcat說? – Razgriz

+0

@Razgriz我已經添加了logcat –

回答

1

我非常確定視圖在生命週期的後期(即調用show())時不會膨脹。

我快速瀏覽了文檔,找不到build()或inflate()方法,但我期望最簡單的方法就是將按鈕操作邏輯前的noteAlert.show()移動到

編輯: 你有沒有嘗試改變:

noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     // some code 
    } 
}); 

noteAlert.setButton(DialogInterface.BUTTON_POSITIVE, "Positive", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     // some code 
    } 
}); 
+0

我剛剛嘗試過。仍然是同樣的問題。 –

+0

你嘗試改變使用setButton()/ setButton方法嗎? 請參閱http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int,java.lang.CharSequence,android.content.DialogInterface.OnClickListener) –

+0

我已經設置了按鈕(setNegativeButton ()等),如果這是你的意思。 –

0

嘗試這種解決方案:

AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this); 
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     // some code 
    } 
}); 
noteAlert.setOnShowListener(new DialogInterface.OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) { 
       if(**some condition**) 
       { 
        Button button = builder.getButton(AlertDialog.BUTTON_POSITIVE); 
        if (button != null) { 
         button.setEnabled(false); 
        } 
       } 
      } 
     });