报错问题:
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。
是主题的问题。
解决方法:
解决方法主要有两种:
- 修改Activity继承的父类:
class MainActivity : Activity()
这种办法最简单。但可能这种办法在工作过程中无法使用。那就看第二种。
- 修改app的主题。
根据提示来使用AppCompat的theme。将AndroidManifest.xml文件中关于Activity的主题配置都要改成如下:
android:theme=“@style/Theme.AppCompat.Light.NoActionBar”
修改之后成功执行。
分析
我很好奇为什么会出现这样的问题。
本来我们的APP继承的主题直接是 @style/Theme.xxxx.
但是在Activity中有内容涉及到了AndroidX相关或者与V7包相关的东西。此时就必须要搭配**@style/Theme.AppCompat.XXX** 使用。