You need to use a Theme.AppCompat theme (or descendant) with this activity(2018-08)

本文详细解析了在Android开发中设置全屏Activity时遇到的IllegalStateException错误原因及解决方案,包括调整Activity继承类和修改主题配置。

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

 

错误原因:

设置全部 Activity 全屏时,进行了如下配置:

<application
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    >

 

错误信息: 

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

android Studio 在执行新建项目时出现此错误,出现此错误的原因就是:

Activty 继承自 androidx.appcompat.app.AppCompatActivity,而不是 android.app.Activty。

 

两种完美的解决办法:

1、相关Activity直接继承Activity,不继承AppCompatActivity

import android.app.Activity;
//public class MainActivity extends AppCompatActivity {
public class MainActivity extends Activity {

 

2、如果必须要继承AppCompatActivity的话,我们看另一种办法

   根据提示来使用AppCompat的theme,即将AndroidManifest.xml文件中关于Activity的主题配置都要改成:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

例如:

<activity
    android:name=".RegisterActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<activity
    android:name=".MainActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

 

参考博文:

https://blog.csdn.net/YuDBL/article/details/8862982

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被开发耽误的大厨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值