androidX You need to use a Theme.AppCompat theme [or descendant] with this activity

androidX You need to use a Theme.AppCompat theme [or descendant] with this activity.


项目整体升级androidx时出现的问题,网上的解决方式都是要么继承activity,要么修改配置theme属性,而我这个问题这两个 方法却没有任何用处.

后来删除项目跟目录下的.gradle和.idea文件后重新编译运行就可以了
我估计原因应该是正式环境混淆打包后引起的本地文件生成问题
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
当在Android应用中遇到"You need to use a Theme.AppCompat theme (or descendant) with this activity."这个错误时,这意味着你在创建或修改某个Activity的XML布局文件时,尝试使用了一个不兼容的的主题。AppCompat主题是Android Support Library的一部分,专为那些使用了较旧API版本但仍然需要新特性(如Action Bar)的应用设计的,它提供了对旧版本API的向下兼容。 解决这个问题通常需要按照以下步骤操作: 1. **检查XML布局**:确认你的Activity标签是否设置了`android:theme`属性,如果设置了,请检查其值是否为`Theme.AppCompat`或其子主题。例如: ```xml <activity android:name=".YourActivity" android:theme="@style/AppTheme"> ``` 如果没有,添加这个主题。 2. **更新样式文件**:如果你有一个单独的`.xml`样式文件(如`styles.xml`),确保`<style>`标签中定义的默认主题或者`<application>`下的主题也指向`Theme.AppCompat`。 3. **继承AppCompatActivity**:如果你的Activity不是直接继承自`Activity`而是其他类,考虑将父类改为`AppCompatActivity`,因为它内部已经包含了AppCompat所需的兼容性增强。 4. **添加依赖**:如果你还没有在build.gradle文件中的dependencies部分添加`appcompat-v7`库,记得添加: ```gradle implementation 'com.android.support:appcompat-v7:version_number' ``` 用实际的版本号替换`version_number`。 5. **运行并检查**:清理并重新构建项目,然后在模拟器或设备上运行,看看错误是否已经被修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值