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

Android ActionBarCompat:java.lang.IllegalStateException:您需要使用Theme.AppCompat,android,android-actionbar,runtimeexception,Android,Android Actionbar,Runtimeexception,我在Android 2.3.5上遇到运行时异常,但我使用Theme.AppCompat(res/values/themes.xml)。这是电话: 对不起,伙计们,我在AndroidManifest.xml中也定义了android:theme=“@style/theme.Styled” 我的清单没有引用任何主题。。。它不应该有问题 当然有。没有任何东西会神奇地将主题.样式化应用于活动。您需要声明您的活动或整个应用程序正在使用Theme.Styled,例如: <application

我在Android 2.3.5上遇到运行时异常,但我使用Theme.AppCompat(res/values/themes.xml)。这是电话:

对不起,伙计们,我在AndroidManifest.xml中也定义了android:theme=“@style/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">
   <activity
        ...
        android:theme="@style/Theme.AppCompat" />
<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>

然后在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.styled且未使用AppCompat theme的值文件夹


ie
values-v11
folder

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


编辑:我建议您停止使用ActionBar,开始使用包含在

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

<style name="Theme.Event" parent="Theme.AppCompat">
   <activity
        ...
        android:theme="@style/Theme.AppCompat" />
<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>
由Chris Banes编写

顺便说一句,对不起我的英语

试试这个

styles.xml

<resources>
 <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
 </style>
</resources>
   <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>

真的
AndroidManifest.xml

<resources>
 <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
 </style>
</resources>
   <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>

您的
活动
正在扩展
ActionBarActivity
,这需要应用
AppCompat.theme

ActionBarActivity
更改为
Activity
FragmentActivity
,这将解决问题。

我在三星设备上发生过这样的崩溃,即使该活动使用了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

我的解决方案是删除android:launchMode=“singleTask”

只需执行构建->清理项目。我想这会解决你的问题。

在我的例子中,我创建了一个自定义视图 我添加到自定义视图构造函数

new RoomView(getAplicationContext());
正确的上下文是活动,因此将其更改为:

new RoomView(getActivity());


para resolver o meu problema,eu apenas adicionei na minha MainActivity(“主题=为了解决我的问题,我刚刚在我的MainActivity(“Theme=“@style/MyTheme”)中添加了它,其中MyTheme是我主题的名称

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

在CustomAdapter类中执行某些操作时,我尝试创建对话框时遇到此错误。 这不是一个活动,而是一个适配器类。 经过36小时的努力和寻找解决方案,我想出了这个

调用CustomAdapter时将活动作为参数发送

CustomAdapter ca = new CustomAdapter(MyActivity.this,getApplicationContext(),records);
在自定义适配器中定义变量

Activity parentActivity;
Context context;
像这样调用构造函数

public CustomAdapter(Activity parentActivity,Context context,List<Record> records){
    this.parentActivity=parentActivity;
    this.context=context;
    this.records=records;
}
AlertDialog ad = new AlertDialog.Builder(parentActivity).setTitle("Your title");

and so on..

我希望这能帮助您

对于我的列表视图,我正在使用扩展ArrayAdapter的自定义适配器。 在ListView中,我有两个按钮,其中一个按钮作为自定义AlertDialogBox。 前任: 活动父母活动; 适配器的构造函数 `


您的清单是否引用了主题.Styled
?是否有另一个引用主题.Styled但不使用AppCompat主题的values文件夹?@tyczj我添加了res/values-v11/themes.xml文件,但它没有引用主题。Styled@tyczj你可以用你的评论作为答案,因为这可能是一个常见的问题(我也是)可能重复的我不正确,我的AndroidManifest.xml中有android:theme=“@style/theme.Styled”(我第一次没有看到它)。太好了,我错过了这个。别忘了所有其他-vXX文件夹,否则它在您的测试环境中会很好地工作,只会在有人使用其中一个版本时咬到您。谢谢!刚刚将ActionBarActivity更改为Activity!:)你有这方面的来源吗?我已经在我的应用程序中跟踪了一个类似的错误有一段时间了,这听起来像是一个很有希望的线索。这里也是!我想了解更多!我也在我的应用程序中跟踪了一个类似的错误。不幸的是,我没有使用
android:launchMode=“singleTask”“
。我观察到同样的问题。Galaxy Tab A 10.1(安卓7.0,非根目录)和SM-A320FL(安卓7.0,非根目录)都是如此。我不使用
Android:launchMode=“singleTask”
我的所有活动都使用AppCompat主题:/这也是我的问题。我有一个库项目,必须导入AppCompat-v7库并使所有主题扩展AppCompat主题。问题是,我的主项目有以下工具:replace=“android:icon,android:theme”添加到的选项应忽略其他项目中的样式。由于某些原因,该选项不起作用。锁定它!升级
public CustomAdapter(ArrayList<Contact> data, Activity parentActivity,Context context) {
        super(context,R.layout.listdummy,data);
        this.mContext   =   context;
        this.parentActivity  =   parentActivity;
    }