升级到AppCompat v22.1.0,现在获得IllegalArgumentException:AppCompat不支持当前的主题功能

本文翻译自:Upgraded to AppCompat v22.1.0 and now getting IllegalArgumentException: AppCompat does not support the current theme features

I've just upgraded my app to use the newly released v22.1.0 AppCompat and I'm now getting the following exception when I open my app. 我刚刚升级了我的应用程序以使用新发布的v22.1.0 AppCompat,现在打开应用程序时出现以下异常。

Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
        at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:360)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)

How do I fix it? 我如何解决它?


#1楼

参考:https://stackoom.com/question/20zl0/升级到AppCompat-v-现在获得IllegalArgumentException-AppCompat不支持当前的主题功能


#2楼

AppCompat is now more strict on what it expect in theme window flags, more closely matching what you would get from the framework. AppCompat现在对主题窗口标志的期望更加严格,与您从框架中获得的匹配更加紧密。

The main reason behind this is to support AppCompatDialogs which we were also adding in this release. 这背后的主要原因是要支持AppCompatDialogs ,我们也在此版本中添加了该功能。 They make heavy use of the windowNoTitle flag, which AppCompat previously didn't pay much attention to. 它们大量使用了windowNoTitle标志,AppCompat以前并未对此进行过多关注。

So to fix your issue you have two options: 因此,要解决您的问题,您有两种选择:

The easy way is to just use Theme.AppCompat.NoActionBar as your parent theme. 简单的方法是只使用Theme.AppCompat.NoActionBar作为父主题。 This will always do the right thing. 这将永远做正确的事。

If you can't do that though (maybe you need to support action bar and no action bar), you should do the following: 如果您还是不能做到这一点(也许您需要支持操作栏而没有操作栏),则应执行以下操作:

<style name="MyTheme" parent="Theme.AppCompat">
    ...
</style>

<style name="MyTheme.NoActionBar">
    <!-- Both of these are needed -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

You should be back on track now. 您现在应该回到正轨。


#3楼

Just use this in your style.xml in values-v21 folder no other editing is needed 只需在values-v21文件夹的style.xml中使用此文件,无需其他编辑

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

    <!-- theme customizations -->

   <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
 </style>

Don't add anything in to activity file please leave it 不要在活动文件中添加任何内容,请保留它

public class Main extends ActionBarActivity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

#4楼

I added 我加了

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

but it wasnt enough. 但这还不够。 Finally, moving super.onCreate before setContentView in Activity - fixed my issue :) 最后,在活动中的setContentView之前移动super.onCreate-解决了我的问题:)

   public void onCreate(Bundle savedInstanceState) {    

        super.onCreate(savedInstanceState);    
        setContentView(R.layout.v2_main_dash);
        ...

#5楼

Those who're still getting error after all of those fix. 那些人在完成所有这些修复后仍然出错。

Please inherit from 请继承

Theme.AppCompat.Light.NoActionBar

and don't use 而且不要使用

<item name="windowActionBar">false</item>

Then, you won't get any error. 这样,您就不会有任何错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值