在对话框中为按钮设置alpha

时间:2012-07-18 02:01:55

标签: android alpha android-alertdialog android-button

我需要在CustomAlertDialog Button上设置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

有什么不对?我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果在对话框中找不到getButton,或者在调用之前未调用方法null,则Button方法将返回show()。 为了避免NullPointerException来电getButton并在后设置字母,请致电show()上的AlertDialog