如何解决:“您需要在此活动中使用 Theme.AppCompat 主题(或后代)" [英] How to fix: "You need to use a Theme.AppCompat theme (or descendant) with this activity"
问题描述
我无法按照视频的说明在全屏模式下运行我的 Android 应用.当它尝试运行时,应用程序崩溃并显示错误.
I am having trouble running my Android app in a fullscreen mode per instructions of a video. When it tries to run, the app crashes with the error.
您需要在此活动中使用 Theme.AppCompat 主题(或后代)
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.djsg38.hikerswatch">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
样式文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
可能有用的部分 MainActivity
Partial MainActivity that may be useful
public class MainActivity extends AppCompatActivity {
推荐答案
你的应用有一个 AppCompat 主题
Your application has an AppCompat theme
<application
android:theme="@style/AppTheme">
但是,您使用不是 AppCompat 主题的后代的主题覆盖了 Activity(它扩展了 AppCompatActivity)
But, you overwrote the Activity (which extends AppCompatActivity) with a theme that isn't descendant of an AppCompat theme
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
您可以像这样定义自己的全屏主题(注意 parent=
中的 AppCompat
)
You could define your own fullscreen theme like so (notice AppCompat
in the parent=
)
<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
然后在活动中设置它.
<activity android:name=".MainActivity"
android:theme="@style/AppFullScreenTheme" >
注意:可能有一个已经全屏的 AppCompat 主题,但不立即知道
Note: There might be an AppCompat theme that's already full screen, but don't know immediately
这篇关于如何解决:“您需要在此活动中使用 Theme.AppCompat 主题(或后代)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!