Applying AlertDialog Styles

I am attempting to customize the AlertDialogs in my app.

When it comes to applying the style, my application crashes

I am only applying a background color, like so…

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

And the Java code…

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();
}

What am I doing wrong?

Edit: Here’s the LogCat as requested:

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

Answer

you may try this ::

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

Leave a Reply

Your email address will not be published. Required fields are marked *