How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity

天涯浪子 提交于 2019-12-03 11:39:19

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.

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>

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..

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!