应用AlertDialog样式

时间:2013-08-30 14:11:09

标签: java android xml android-alertdialog

我正在尝试在我的应用中自定义AlertDialogs。

在应用样式时,我的应用程序崩溃了

我只使用背景颜色,如此...

<style name="fd_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:background">#FFFFFF</item>
</style>

Java代码......

public void quit(View v)
{
    AlertDialog.Builder confirm = new AlertDialog.Builder(this, R.style.fd_dialog); 
    confirm.setTitle("Quit?");
    confirm.setIcon(R.drawable.questionicon);
    confirm.setMessage("Are You Sure You Want To Log Out?");
    confirm.setPositiveButton("Quit", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            //do some stuff here
        }
    });
    confirm.setNegativeButton("Cancel", null);
    confirm.show();
}

我做错了什么?

编辑:这是LogCat的要求:

08-30 15:25:27.349: W/dalvikvm(2020): threadid=1: thread exiting with uncaught exception (group=0x40018578)

08-30 15:25:27.359: E/AndroidRuntime(2020): FATAL EXCEPTION: main

08-30 15:25:27.359: E/AndroidRuntime(2020): java.lang.IllegalStateException: Could not execute method of the activity

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.view.View$1.onClick(View.java:2144)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.view.View.performClick(View.java:2485)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.view.View$PerformClick.run(View.java:9080)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.os.Handler.handleCallback(Handler.java:587)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.os.Handler.dispatchMessage(Handler.java:92)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.os.Looper.loop(Looper.java:130)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.app.ActivityThread.main(ActivityThread.java:3687)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at java.lang.reflect.Method.invokeNative(Native Method)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at java.lang.reflect.Method.invoke(Method.java:507)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at dalvik.system.NativeStart.main(Native Method)
08-30 15:25:27.359: E/AndroidRuntime(2020): Caused by: java.lang.reflect.InvocationTargetException

08-30 15:25:27.359: E/AndroidRuntime(2020):     at java.lang.reflect.Method.invokeNative(Native Method)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at java.lang.reflect.Method.invoke(Method.java:507)

08-30 15:25:27.359: E/AndroidRuntime(2020):     at android.view.View$1.onClick(View.java:2139)

08-30 15:25:27.359: E/AndroidRuntime(2020):     ... 11 more

08-30 15:25:27.359: E/AndroidRuntime(2020): Caused by: java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.<init>

08-30 15:25:27.359: E/AndroidRuntime(2020):     at com.firsthosted.firstdroid.Home.quit(Home.java:537)

8-30 15:25:27.359: E/AndroidRuntime(2020):  ... 14 more

2 个答案:

答案 0 :(得分:0)

检查这是否有帮助。而不是

AlertDialog.Builder confirm = new AlertDialog.Builder( this ,R.style.fd_dialog);使用这个

AlertDialog.Builder confirm = new AlertDialog.Builder( youractivity.this ,R.style.fd_dialog);

答案 1 :(得分:0)

你可以试试这个::

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.fd_dialog));