ActionBarCompat:java.lang.IllegalStateException:您需要使用Theme.AppCompat

AG1*_*AG1 102 android runtimeexception android-actionbar

我得到在Android 2.3.5一个RuntimeException,但我正在用Theme.AppCompat(RES /价值/的themes.xml).这是电话:http://www.gsmarena.com/samsung_galaxy_y_s5360-4117.php

 <!-- res/values/themes.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <resources>

     <style name="Theme.Styled" parent="@style/Theme.AppCompat">
         <item name="actionBarStyle">@style/QueryActionBar</item>
         <item name="android:actionBarStyle">@style/QueryActionBar</item>
     </style>

     <style name="QueryActionBar" parent="@style/Widget.AppCompat.ActionBar">
         <item name="background">@color/blueback</item>
         <item name="android:background">@color/blueback</item>
         <item name="backgroundSplit">@color/blueback</item>
         <item name="android:backgroundSplit">@color/blueback</item>
     </style>

 </resources>
Run Code Online (Sandbox Code Playgroud)

这是values-v11的文件.

 <!-- res/values-v11/themes.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <style name="QueryTheme" parent="@android:style/Theme.Holo">
    <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
 </resources>
Run Code Online (Sandbox Code Playgroud)

这是错误.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txt2lrn.www/com.txt2lrn.www.LandingActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
 at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:130)
 at android.app.ActivityThread.main(ActivityThread.java:3687)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
 at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)
 at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
 at com.txt2lrn.www.LandingActivity.onCreate(LandingActivity.java:95)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
 ... 11 more
Run Code Online (Sandbox Code Playgroud)

对不起朋友们,我也确实在AndroidManifest.xml中定义了android:theme ="@ style/Theme.Styled".

Nis*_*had 96

如果要在MainActivity中扩展ActionBarActivity,则还必须在values-v11中更改父主题.
所以values-v11中的style.xml将是 -

 <!-- res/values-v11/themes.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <style name="QueryTheme" parent="@style/Theme.AppCompat">
    <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
 </resources>
Run Code Online (Sandbox Code Playgroud)

编辑:我建议你停止使用ActionBar并开始使用Android设计支持库中包含的AppBar布局

  • 很好,我错过了这个.不要忘记所有其他-vXX文件夹,或者它在你的测试环境中工作正常,只有在有人使用其中一个版本时才会咬你. (5认同)

mol*_*oka 66

要简单地添加ActionBar Compat,您的活动或应用程序应该在AndroidManifest.xml中使用@ style/Theme.AppCompat主题,如下所示:

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

这将在activty中添加actionbar(如果您将此主题添加到应用程序,则添加所有活动)


但通常你需要自定义动作栏.为此,您需要使用Theme.AppCompat父级创建两个样式,例如"@ style/Theme.AppCompat.Light".第一个将是api 11> =(在android操作栏中构建的android版本),第二个用于api 7-10(没有在操作栏中构建).

让我们来看看第一种风格.它将位于res/values-v11/styles.xml中.它看起来像这样:

<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the android namespace affects API levels 11+ -->
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the android namespace affects API levels 11+ -->
    <item name="android:background">@drawable/ab_custom_solid_styled</item>
    <item name="android:backgroundStacked"
      >@drawable/ab_custom_stacked_solid_styled</item>
    <item name="android:backgroundSplit"
      >@drawable/ab_custom_bottom_solid_styled</item>
</style>
Run Code Online (Sandbox Code Playgroud)

你需要为api 7-10拥有相同的风格.它将位于res/values/styles.xml中,但因为api级别还不知道原始的android操作栏样式项,我们应该使用一个,由支持库提供.ActionBar Compat项目的定义与原始android类似,但前面没有"android:"部分:

<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the default namespace affects API levels 7-11 -->
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the default namespace affects API levels 7-11 -->
    <item name="background">@drawable/ab_custom_solid_styled</item>
    <item name="backgroundStacked">@drawable/ab_custom_stacked_solid_styled</item>
    <item name="backgroundSplit">@drawable/ab_custom_bottom_solid_styled</item>
</style>
Run Code Online (Sandbox Code Playgroud)

请注意,即使api级别高于10已经有动作栏,你仍然应该使用AppCompat样式.如果不这样做,那么在Android 3.0及更高版本的设备上启动Acitvity时会出现此错误:

java.lang.IllegalStateException:您需要将Theme.AppCompat主题(或后代)与此活动一起使用.

这是由Chris Banes撰写的原始文章http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html的链接.

PS抱歉我的英文


tyc*_*czj 20

检查并确保您没有引用theme.styled的其他值文件夹,并且不使用AppCompat主题

values-v11文件夹


Sil*_*uti 16

试试这个...

styles.xml

<resources>
 <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
 </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

   <activity
        android:name="com.example.Home"
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)


Jor*_*sys 11

Activity正在扩展ActionBarActivity,需要AppCompat.theme应用.从更改ActionBarActivityActivityFragmentActivity,它会解决这个问题.


Com*_*are 3

我的清单没有引用任何主题...它不应该是 AFAIK

当然可以。没有什么会神奇地适用Theme.Styled于一项活动。您需要声明您的活动 - 或您的整个应用程序 - 正在使用Theme.Styled,例如:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Styled">
Run Code Online (Sandbox Code Playgroud)