16
votes

I searched all internet web sites to fix this error, but I couldn't. I just want to create AlertDialog with two button Yes and No.

This is my code:

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class DialogActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);

    Button btnDialog= (Button) findViewById(R.id.btnDialog);
    btnDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialogMessage();
        }
    });
}

private void showDialogMessage(){

    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    // Add the buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
        }
    });
    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
}
}

And this is my Style:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

this is my style(v21):

<resources>
<!-- extend one of the Theme.AppCompat themes -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- customize the color palette -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:colorControlHighlight">@color/colorHighLight</item>
</style>
</resources>

this is manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.saly.rastari" >   
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".DialogActivity"
            android:label="@string/title_activity_test"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>       
    </application>
</manifest>

When I click on Dialog button I get this error:

11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: FATAL EXCEPTION: main
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: Process: com.irannara.pda.pdaassistant, PID: 24352
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AlertController.installContent(AlertController.java:216)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:274)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at com.irannara.pda.pdaassistant.DialogActivity.showDialogMessage(DialogActivity.java:42)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at com.irannara.pda.pdaassistant.DialogActivity.access$000(DialogActivity.java:10)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at com.irannara.pda.pdaassistant.DialogActivity$1.onClick(DialogActivity.java:21)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4780)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19866)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-02 04:36:55.959 718-718/? E/EGL_emulation: tid 718: eglCreateSyncKHR(1209): error 0x3004 (EGL_BAD_ATTRIBUTE)

none of this links couldn't help me:

and more.

how can i fix this?

6
show us your manifest to help youMoudiz
Did you try using support AlertDialog?Arslan Ashraf
yes import android.support.v7.app.AlertDialog;Salar Rastari

6 Answers

35
votes

If you have another styles files in side another values folders like "values-v11", "values-v14"... Edit theme also and try to clean your app before running.

Edited: From your activity change getApplicationContext() to this:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

to

AlertDialog.Builder builder = new AlertDialog.Builder(this);

Because the dialog also should extends the Appcompat Theme.

1
votes

If anybody experiences this issue with activities, try to explicitly configure a theme to your activity.

<activity android:name=".activities.BLEControlActivity" android:theme="@style/Theme.AppCompat.DayNight"></activity>
1
votes

You are encountering this error because your AlertDialog is not using AppCompat theme while your activity is. To fix it, use the this property of your activity instead of getApplicationContext(), like so:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);

The name of your activity, followed by .this, in this case DialogActivity.this, should always be used instead of only this, because if your dialog is created inside another class, for example an adapter of some sort, you will receive a compile-time error stating that a Context was expected instead of the adapter class given.

0
votes

in your style.xml file add below code-

style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme.Base" 
           parent="@style/Theme.AppCompat.Light">
    </style>
</resources>

also define theme like this in your activity

 <activity
            android:name=".DialogActivity"
            android:label="@string/title_activity_test"
            android:screenOrientation="portrait" 
            android:theme="@android:style/AppTheme.Dialog">

OR

<activity
            android:name=".DialogActivity"
            android:label="@string/title_activity_test"
            android:screenOrientation="portrait" 
            android:theme="@style/AppTheme">

clean project and run again..

0
votes

I face such a case and I was managed to solve it. Here it is:

  • define your Alert Dialog in MainActivity class, example:

public class MainActivity extends AppCompatActivity {

   AlertDialog.Builder alertDialog;
  • then initialize it on OnCreate() method just like below:

alertDialog = new AlertDialog.Builder(this);
  • then you can do the rest of alert Dialog customization like icon, title, message anywhere you like. in my case i used it in onFinish() of count down timer as shown below:

public void onFinish() {

                alertDialog.setIcon(android.R.drawable.alert_light_frame);
                alertDialog.setTitle("You are done");
                alertDialog.setMessage("you got it");
                alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        runTheCode();
                    }
                });
                alertDialog.show();
-1
votes

Try adding following to your proguard-rules:

-keep public class com.google.android.gms.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

-dontwarn com.google.android.gms.**

If you are using

com.google.android.gms:play-services-ads:8.1.0

Have a look at Google Issue 190237