ActionBarCompat:java.lang.IllegalStateException:你需要使用一个Theme.AppCompat

我在Android 2.3.5上得到一个RuntimeException,但我使用Theme.AppCompat(res / values / 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> 

这是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> 

这是错误。

  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 

抱歉的人,我也有AndroidManifest.xml中定义的android:theme =“@ style / Theme.Styled”。

如果要在您的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> 

编辑:我会build议你停止使用ActionBar,并开始使用Androiddevise支持库中包含的AppBar布局

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

  <activity ... android:theme="@style/Theme.AppCompat" /> 

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


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

我们来看第一种风格。 它将位于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> 

而你需要有相同的风格的API 7-10。 它将位于res / values / styles.xml中 ,但是因为api级别还不知道原始android actionbar风格项目,我们应该使用一个,由支持库提供。 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> 

请注意,即使api级别高于10,已经有了actionbar,你仍然应该使用AppCompat风格。 如果你不这样做,你将在Android 3.0和更高版本的设备上启动Acitvity时出现这个错误:

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

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

PS对不起,我的英语

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

values-v11文件夹

尝试这个…

styles.xml

 <resources> <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light"> <item name="android:windowNoTitle">true</item> </style> </resources> 

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> 

您的Activity正在扩展ActionBarActivity ,这需要应用AppCompat.theme 。 从ActionBarActivity更改为ActivityFragmentActivity ,它将解决问题。

只要做build设 – >清洁工程。 我想这会解决你的问题。

我的清单没有提及任何主题…它不应该AFAIK

当然是的。 没有什么会神奇地将Theme.Styled应用于一个活动。 您需要声明您的活动 – 或者您的整个应用程序 – 使用Theme.Styled ,例如:

 <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Styled"> 

我只是让我的应用程序从ActionBarSherlock移动到ActionBarCompat。 尝试像这样宣布你的旧主题:

 <style name="Theme.Event" parent="Theme.AppCompat"> 

然后在AndroidManifest.xml中设置主题:

 <application android:debuggable="true" android:name=".activity.MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Event.Home" > 

即使该活动确实使用了Theme.AppCompat,我仍然在三星设备上发生了这样的崩溃。 根本原因与三星方面奇怪的优化有关:

 - if one activity of your app has theme not inherited from Theme.AppCompat - and it has also `android:launchMode="singleTask"` - then all the activities that are launched from it will share the same Theme 

我的解决scheme是刚刚删除android:launchMode="singleTask"

当我在CustomAdapter类中执行某个操作时,我正试图创build一个DialogBox时遇到了这个错误。 这不是一个活动,而是一个适配器类。 经过36小时的努力,寻找解决scheme,我想出了这个。

在调用CustomAdapter时将Activity作为参数发送。

 CustomAdapter ca = new CustomAdapter(MyActivity.this,getApplicationContext(),records); 

在自定义适配器中定义variables。

 Activity parentActivity; Context context; 

像这样调用构造函数。

 public CustomAdapter(Activity parentActivity,Context context,List<Record> records){ this.parentActivity=parentActivity; this.context=context; this.records=records; } 

最后,在适配器类中创build对话框时,就像这样做。

 AlertDialog ad = new AlertDialog.Builder(parentActivity).setTitle("Your title"); and so on.. 

我希望这可以帮助你

在我的情况下,我做了一个自定义视图我添加到自定义视图构造函数

 new RoomView(getAplicationContext()); 

正确的上下文是活动,所以将其更改为:

 new RoomView(getActivity()); 

要么

 new RoomView(this); 

对于我的列表视图使用扩展ArrayAdapter的自定义适配器。 在listiview我有2个button之一的button作为自定义AlertDialogBox。 例如:活动parentActivity; 适配器`的构造函数

 public CustomAdapter(ArrayList<Contact> data, Activity parentActivity,Context context) { super(context,R.layout.listdummy,data); this.mContext = context; this.parentActivity = parentActivity; } 

从MainActivty调用适配器

 adapter = new CustomAdapter(dataModels,MainActivity.this,this); 

(“主题=解决我的问题,我刚刚添加它在我的MainActivity(”主题=“@风格/ MyTheme”“),其中MyTheme是我的主题的名称

 [Activity(Label = "Name Label", MainLauncher = true, Icon = "@drawable/icon", LaunchMode = LaunchMode.SingleTop, Theme = "@style/MyTheme")]