如何修复:"You need to use a Theme.AppCompat theme (or descendant) with this activity"
How to fix: "You need to use a Theme.AppCompat theme (or descendant) with this activity"
我无法按照视频说明在全屏模式下 运行 设置我的 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 {
您的应用程序有一个 AppCompat 主题
<application
android:theme="@style/AppTheme">
但是,您用一个不是 AppCompat 主题的后代的主题覆盖了 Activity(它扩展了 AppCompatActivity)
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
您可以像这样定义自己的全屏主题(注意 AppCompat
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 上设置。
<activity android:name=".MainActivity"
android:theme="@style/AppFullScreenTheme" >
注意: 可能有一个 AppCompat 主题已经全屏了,但现在还不知道
如果您将 android:theme="@style/Theme.AppCompat.Light"
添加到 AndroidManifest.xml 文件中的 <application>
,问题正在解决。
你应该在你的所有活动中添加一个 theme
(你应该在你的清单中为你的 <application>
中的所有应用程序添加 theme
)
但是如果你为你的 activity 设置了不同的主题,你可以使用 :
android:theme="@style/Theme.AppCompat"
或各种AppCompat
主题!
曾经遇到过同样的问题。原因是传递给 AlertDialog.Builder(here)
的上下文不正确。使用像
AlertDialog.Builder(Homeactivity.this)
当您在 activity 中使用 AlertDialog 时会出现此错误。如果你想得到这个问题的解决方案,你只需要确保你想在哪个 activity 中显示 AlertDialog 弹出窗口,这个上下文必须像 Demo.this。永远不要使用 getApplicationContext.
我在尝试设置 react-native-bootsplash 时遇到此错误,在他们的文档中他们说你必须从这个
更改你的应用程序主题
<application
...
android:theme="@style/AppTheme">
至此
<application
...
android:theme="@style/BootTheme">
但对我有用的是将应用程序主题保留为 android:theme="@style/AppTheme"
并更改 activity 主题,如下所示:
<activity
android:name=".SplashActivity"
android:theme="@style/BootTheme"/> <!-- HERE -->
面临同样的问题。
如果您的应用程序正在使用 Theme.MaterialComponents......
然后其他地方如果使用@android:style/Theme..那么这个问题就面临了。
请注意,在您的项目中 /values/
和 /values-night/
中可能有 2 个 styles.xml
,因为当 OS 设置为浅色或深色模式时。在这两种情况下,您都需要将默认主题替换为 AppCompat
主题,以防您启用了 Dark 设置。有时只在晚上开启,所以你的应用程序可能在白天编译但在特定时间后停止工作,导致很多调试混乱和头痛。这也导致它仅在某些设备上失败(例如 sim vs real):
旧
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
新
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
我无法按照视频说明在全屏模式下 运行 设置我的 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 {
您的应用程序有一个 AppCompat 主题
<application
android:theme="@style/AppTheme">
但是,您用一个不是 AppCompat 主题的后代的主题覆盖了 Activity(它扩展了 AppCompatActivity)
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
您可以像这样定义自己的全屏主题(注意 AppCompat
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 上设置。
<activity android:name=".MainActivity"
android:theme="@style/AppFullScreenTheme" >
注意: 可能有一个 AppCompat 主题已经全屏了,但现在还不知道
如果您将 android:theme="@style/Theme.AppCompat.Light"
添加到 AndroidManifest.xml 文件中的 <application>
,问题正在解决。
你应该在你的所有活动中添加一个 theme
(你应该在你的清单中为你的 <application>
中的所有应用程序添加 theme
)
但是如果你为你的 activity 设置了不同的主题,你可以使用 :
android:theme="@style/Theme.AppCompat"
或各种AppCompat
主题!
曾经遇到过同样的问题。原因是传递给 AlertDialog.Builder(here)
的上下文不正确。使用像
AlertDialog.Builder(Homeactivity.this)
当您在 activity 中使用 AlertDialog 时会出现此错误。如果你想得到这个问题的解决方案,你只需要确保你想在哪个 activity 中显示 AlertDialog 弹出窗口,这个上下文必须像 Demo.this。永远不要使用 getApplicationContext.
我在尝试设置 react-native-bootsplash 时遇到此错误,在他们的文档中他们说你必须从这个
更改你的应用程序主题<application
...
android:theme="@style/AppTheme">
至此
<application
...
android:theme="@style/BootTheme">
但对我有用的是将应用程序主题保留为 android:theme="@style/AppTheme"
并更改 activity 主题,如下所示:
<activity
android:name=".SplashActivity"
android:theme="@style/BootTheme"/> <!-- HERE -->
面临同样的问题。 如果您的应用程序正在使用 Theme.MaterialComponents...... 然后其他地方如果使用@android:style/Theme..那么这个问题就面临了。
请注意,在您的项目中 /values/
和 /values-night/
中可能有 2 个 styles.xml
,因为当 OS 设置为浅色或深色模式时。在这两种情况下,您都需要将默认主题替换为 AppCompat
主题,以防您启用了 Dark 设置。有时只在晚上开启,所以你的应用程序可能在白天编译但在特定时间后停止工作,导致很多调试混乱和头痛。这也导致它仅在某些设备上失败(例如 sim vs real):
旧
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
新
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">