Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant)

本文介绍了在Android开发中遇到的AppTheme冲突问题及其解决方案。作者在使用自定义AppTheme时遇到了IllegalStateException异常,通过排查发现是由于多个模块中存在同名的AppTheme导致的。最终通过删除其他模块中的重复主题解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天碰到了这么一个问题, Caused by: Java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this 根据日志,大意是父类用了AppTheme这个主题,这个子类Activity也必须是这个Theme,否则加载这个Activity的时候不知道用父类的Theme还是用子类的Theme。

由于自己在Manifest的application标签中声明了一个自定义的mainthem主题样式,它继承一个我自定义的appthem,

<application
    android:name=".LancentApplication"
    android:allowBackup="true"
    android:icon="@drawable/greatwall_logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    tools:replace="android:icon"
    android:theme="@style/MainTheme">

<style name="MainTheme" parent="AppTheme">

其他的activity也都用这个样式,但是提示登录页面这个错误,我就改了登陆的them,登录页面正常了,但又提示主页面这个错误.依次改了好多,发现改的页面好了,但后面一个页面就不行了.

找了好久终于找到原因了,最后发现原因是在安卓studio的工程中,其他moudle中的style文件中和我自定义继承的appthem同名字的appthem,但这个appthem不是我自己定义的,所以在编译时虚拟机不能识别哪一个appthem是正确的,我就把moudle里的them删除掉,我的工程就正常了.


E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hjschoolhelper210301201_1, PID: 2778 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hjschoolhelper210301201_1/com.example.hjschoolhelper210301201_1.LoginMainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:846) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) at com.example.hjschoolhelper210301201_1.LoginMainActivity.onCreate(LoginMainActivity.java:26) at android.app.Activity.performCreate(Activity.java:8305) at android.app.Activity.performCreate(Activity.java:8284) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:201)  at android.os.Looper.loop(Looper.java:288)  at android.app.ActivityThread.main(ActivityThread.java:7872)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
05-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值