【问题标题】:Unable to start MainActivity - You need to use a Theme.AppCompat theme (or descendant) with this activity无法启动 MainActivity - 您需要在此活动中使用 Theme.AppCompat 主题(或后代)
【发布时间】:2025-12-22 04:25:07
【问题描述】:

当我通过 Android Studio(调试)在手机上运行我的应用时,它可以正常工作。

但是每当我尝试创建一个签名的发布 APK 并在我的手机上安装发布 APK 时,它会立即崩溃,并在 logcat 中显示此错误:

java.lang.IllegalStateException 您需要在此活动中使用 Theme.AppCompat 主题(或后代)

根据 logcat,错误发生在 MainActivity 的 onCreate 方法期间。

我查看了许多 * 线程以及其他网站和论坛尝试解决方案,但到目前为止没有一个对我有用。

例如,其中一些不起作用:

  • android:theme="@style/Theme.AppCompat.Light" 添加到AndroidManifest.xml 文件中的应用程序标记中。
  • 更改应用主题的名称(默认为 AppTheme)
  • 使用 Activity 而不是 AppCompatActivity(我必须为此使用 AppCompatActivity

我所有的 Activity .java 类都扩展了 AppCompatActivity,而我的 AppTheme 扩展了 Theme.AppCompat.Light.DarkActionBar,所以我不知道为什么会这样。

这是代码(希望这是所有相关代码):

(注意:只有 values/styles.xml,没有 values-11、values-14 等)

styles.xml:

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


    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.drawsmile.mealsonandroid" >

        <!--
             The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
             Google Maps Android API v2, but you must specify either coarse or fine
             location permissions for the 'MyLocation' functionality. 
        -->

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

        <application
            android:name="android.support.multidex.MultiDexApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" >
            android:supportsRtl="true"

            android:theme="@style/AppTheme"
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <!--
                 The API key for Google Maps-based APIs is defined as a string resource.
                 (See the file "res/values/google_maps_api.xml").
                 Note that the API key is linked to the encryption key used to sign the APK.
                 You need a different API key for each encryption key, including the release key that is used to
                 sign the APK for publishing.
                 You can define the keys for the debug and release targets in src/debug/ and src/release/. 
            -->
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_key" />

            <activity
                android:name=".MapsActivity"
                android:label="@string/title_activity_maps" />
            <activity android:name=".AuthenticationActivity"/>

            <service android:name=".FirebaseCMService" >
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
        </application>

    </manifest>

activity_main.xml:

android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="org.drawsmile.mealsonandroid.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:paddingTop="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@android:color/holo_red_light"
        android:textColor="#fff"
        android:paddingTop="8dp"
        android:paddingBottom="8dp" />

</android.support.v4.view.ViewPager>

MainActivity.java

package org.drawsmile.mealsonandroid;

...

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

...

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

...

}

【问题讨论】:

    标签: java android android-layout android-theme androiddesignsupport


    【解决方案1】:

    在您的Manifest 中,您将在application 标记之外应用主题,请参见下文。

    <application
            android:name="android.support.multidex.MultiDexApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" >
            android:supportsRtl="true"
    
            android:theme="@style/AppTheme"
    

    改成这个

    <application
            android:name="android.support.multidex.MultiDexApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" 
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    

    【讨论】:

    • 我相信他在复制和粘贴问题时是一个错字。但是 +1 抓住它。我报告这太本地化/无法复制。
    • 哇,我很笨。那行得通。这不是我创建问题时的拼写错误。
    • @p00pClan 很高兴为您提供帮助。
    【解决方案2】:

    在您的 styles.xml 替换中,编辑 AppTheme

    “Theme.AppCompat.Light.DarkActionBar”

    “Theme.AppCompat.Light.NoActionBar”

    ,这应该可以解决您的问题。

    原因是,您正在尝试覆盖操作栏(可能),但应用程序已经有操作栏。希望对你有帮助

    【讨论】:

    • 当你所说的发生时它不会崩溃。它将显示 2 个操作栏