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.
在这里插入图片描述

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

在这里插入图片描述

报错原因:

Activty继承自android.support.v7.app.AppCompatActivty,而不是android.app.Activty。
是主题的问题。

解决方法:

解决方法主要有两种:

  1. 修改Activity继承的父类:

class MainActivity : Activity()
这种办法最简单。但可能这种办法在工作过程中无法使用。那就看第二种。

  1. 修改app的主题。
    根据提示来使用AppCompat的theme。将AndroidManifest.xml文件中关于Activity的主题配置都要改成如下:

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

修改之后成功执行。

分析

我很好奇为什么会出现这样的问题。
本来我们的APP继承的主题直接是 @style/Theme.xxxx.
但是在Activity中有内容涉及到了AndroidX相关或者与V7包相关的东西。此时就必须要搭配**@style/Theme.AppCompat.XXX** 使用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值