如何修复:"你需要在这个活动中使用Theme.AppCompat主题(或后代)"

Dou*_*ert 29 android android-appcompat appcompatactivity

根据视频说明,我无法在全屏模式下运行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>
Run Code Online (Sandbox Code Playgroud)

样式文件

<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>
Run Code Online (Sandbox Code Playgroud)

可能有用的部分MainActivity

public class MainActivity extends AppCompatActivity {
Run Code Online (Sandbox Code Playgroud)

cri*_*007 83

您的应用程序具有AppCompat主题

<application
    android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)

但是,你用一个不是AppCompat主题后代的主题覆盖了Activity(扩展了AppCompatActivity)

<activity android:name=".MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
Run Code Online (Sandbox Code Playgroud)

你可以定义自己的全屏的主题,像这样(通知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>
Run Code Online (Sandbox Code Playgroud)

然后在Activity上设置它.

<activity android:name=".MainActivity"
    android:theme="@style/AppFullScreenTheme" >
Run Code Online (Sandbox Code Playgroud)

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


小智 39

曾经面临同样的问题。原因是在不正确的上下文中传递给AlertDialog.Builder(here). 使用像 AlertDialog.Builder(Homeactivity.this)

  • 呃,这也是给我的。我传递的是应用程序上下文而不是活动上下文。错误消息并不表明这是问题所在。谢谢。 (3认同)

san*_*adi 9

你应该theme为你的所有活动添加一个(你应该在你的清单中theme为你的所有应用程序添加<application>)但是如果你为你的活动设置了不同的主题,你可以使用:

 android:theme="@style/Theme.AppCompat"
Run Code Online (Sandbox Code Playgroud)

或各种AppCompat主题!


小智 5

如果您将其添加android:theme="@style/Theme.AppCompat.Light"<application>AndroidManifest.xml 文件中,问题就解决了。