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

后端 未结 6 1559
醉梦人生
醉梦人生 2021-02-07 09:36

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:

impo         


        
相关标签:
6条回答
  • 2021-02-07 09:59

    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>
    
    0 讨论(0)
  • 2021-02-07 10:07

    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

    0 讨论(0)
  • 2021-02-07 10:08

    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 讨论(0)
  • 2021-02-07 10:18

    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 讨论(0)
  • 2021-02-07 10:18

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

    0 讨论(0)
  • 2021-02-07 10: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.

    0 讨论(0)
提交回复
热议问题