如何解决:“您需要在此活动中使用 Theme.AppCompat 主题(或后代)”

新手上路,请多包涵

根据视频说明,我无法以全屏模式运行我的 Android 应用程序。当它尝试运行时,应用程序因错误而崩溃。

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

清单文件

<?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

 public class MainActivity extends AppCompatActivity {

原文由 Doug Steiert 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

您的应用程序具有 AppCompat 主题

<application
    android:theme="@style/AppTheme">

但是,您使用不是 AppCompat 主题后代的主题覆盖了 Activity(它扩展了 AppCompatActivity)

 <activity android:name=".MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

您可以像这样定义自己的全屏主题(注意 AppCompatparent= 中)

 <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上设置。

 <activity android:name=".MainActivity"
    android:theme="@style/AppFullScreenTheme" >

注意:可能有一个 AppCompat 主题已经全屏,但不立即知道

原文由 OneCricketeer 发布,翻译遵循 CC BY-SA 3.0 许可协议

请注意,当操作系统设置设置为浅色或深色模式时,在您的项目中可能有 2 styles.xml/values//values-night/ 中。在这两种情况下,您都需要将默认主题替换为 AppCompat 主题,以防您开启了深色设置。有时只在晚上开启,所以您的应用程序可能会在白天编译但在特定小时后停止工作,从而导致大量调试混乱和头痛。这也导致它仅在某些设备中失败(例如,sim vs real):

老的

<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">

新的

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

原文由 eriel marimon 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题