2012-07-18 71 views
-1

我需要在CustomAlertDialog Button上設置Alpha。我嘗試這樣做:在對話框中爲按鈕設置alpha

AlertDialog userdialog = new AlertDialog.Builder(mActivity) 
        .create(); 
    userdialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int whichButton) { 
        // Snippet: Do sth. 
      return; 
      } 
     }); 

userdialog.getButton(AlertDialog.BUTTON_POSITIVE).getBackground().setAlpha(100); 

但我得到這個錯誤:

07-18 08:57:43.831: E/AndroidRuntime(4955): FATAL EXCEPTION: main 
07-18 08:57:43.831: E/AndroidRuntime(4955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ttet.tsquare.pure/com.ttet.tsquare.pure.Map}: java.lang.NullPointerException 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.os.Looper.loop(Looper.java:123) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at dalvik.system.NativeStart.main(Native Method) 
07-18 08:57:43.831: E/AndroidRuntime(4955): Caused by: java.lang.NullPointerException 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at com.ttet.tsquare.pure.Dialog.show(Dialog.java:160) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at com.ttet.tsquare.pure.Map.onCreate(Map.java:63) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-18 08:57:43.831: E/AndroidRuntime(4955):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-18 08:57:43.831: E/AndroidRuntime(4955): 

... 11 more 

錯了呢?我該怎麼辦?

+0

你在'160'行的'Dialog'類的'show'方法中有'NullPointerException'。 – Luksprog 2012-07-18 05:19:10

+0

我知道,但是我如何在沒有得到這個異常的情況下引用按鈕。這纔是重點。 – majorl3oat 2012-07-18 06:21:11

回答

1

getButton方法將返回null如果Button不能對話框或方法show()中找到沒有被其調用之前調用。 爲了避免NullPointerException致電getButton並在之後設置了字母,請撥打 AlertDialog

+0

非常感謝,這些讓我更瞭解警報對話的更多步驟。 – majorl3oat 2012-07-18 07:10:57

+0

對不起,我的英語語法,這不是我的本地人。 – majorl3oat 2012-07-18 07:11:17

+0

我還有一個問題。我成功設置按鈕alpha,並使其無法點擊,但是當我做了一些事情後,我必須再次設置alpha並使其可點擊。它工作正常,但界面不會改變。我如何刷新對話界面? – majorl3oat 2012-07-18 07:23:15

相關問題