解决You need to use a Theme.AppCompat theme (or descendant) with this activity

简介: 解决You need to use a Theme.AppCompat theme (or descendant) with this activity

错误信息

2019-07-24 15:35:40.385 18490-18490/com.adojayfan.rntest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.adojayfan.rntest, PID: 18490
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:552)
        at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
        at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
        at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:407)
        at android.app.Dialog.show(Dialog.java:302)
        at com.adojayfan.rntest.utils.UiTools.showAlertDialog(UiTools.java:266)
        at com.adojayfan.rntest.MainActivity$MyWebChromeClient.onJsConfirm(MainActivity.java:116)
        at com.tencent.smtt.sdk.f.onJsConfirm(SmttWebChromeClient.java:149)
        at com.tencent.tbs.core.webkit.tencent.TencentWebChromeClient.onJsConfirm(TbsJavaCore:159)
        at android.webview.chromium.WebViewContentsClientAdapter.handleJsConfirm(TbsJavaCore:889)
        at org.chromium.android_webview.AwContentsClientBridge$4.run(TbsJavaCore:260)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

解决方式

  1. 首先查看当前Activity的主题。如果Activity继承的是AppCompatActivity,那么主题必须继承自Theme.AppCompat。
<activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:hardwareAccelerated="true"
            android:theme="@style/Theme.AppCompat.Light">
  1. 如果确定Activity的主题没问题,那可能是因为使用Dialog时传入的Context不对。
    Dialog需要的Context不能使用getApplicationContext()。
//错误
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
//正确
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
相关文章
|
Android开发
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
120 0
|
开发工具 Android开发
细说 AppCompat 主题引发的坑:You need to use a Theme.AppCompat theme with this activity!
细说 AppCompat 主题引发的坑:You need to use a Theme.AppCompat theme with this activity!
细说 AppCompat 主题引发的坑:You need to use a Theme.AppCompat theme with this activity!
|
Android开发
You need to use a Theme.AppCompat theme (or descendant) with this activity
You need to use a Theme.AppCompat theme (or descendant) with this activity
262 0
|
XML Android开发 数据格式
|
Android开发
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
566 0
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
|
Android开发 容器
Android ViewPager和PagerAdapter简单代码写法
Android ViewPager和PagerAdapter简单代码写法 总是忘记,记下来备忘: package zhangphil.
1531 0
|
XML Android开发 数据格式