欢迎各位访问的博客,博客地址

配置项目主题的时候报了如下错误:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR

原因一

错误写法:

其中AppTheme使用的主题是AppCompat的主题,由于AppCompat主题下的windowActionBar和windowNoTitle的命名方式前都没有android字样,所以报错。

正确写法:

原因二

如果主题设置成有Actionbar的Theme并且没有配:

"windowActionBar">false"windowNoTitle">true

也会出这样的错误。

看下源码:

在我们设置toolbar时候:((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);点进源码可以看到源码调用逻辑是:

public void setSupportActionBar(@Nullable Toolbar toolbar) {        getDelegate().setSupportActionBar(toolbar);    }

在往下追一步,出真相了:

public void setSupportActionBar(Toolbar toolbar) {        if (!(mOriginalWindowCallback instanceof Activity)) {            // Only Activities support custom Action Bars            return;        }        //这里会去判有没有actionbar存在,如果有直接抛异常        final ActionBar ab = getSupportActionBar();        if (ab instanceof WindowDecorActionBar) {            throw new IllegalStateException("This Activity already has an action bar supplied " +                    "by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set " +                    "windowActionBar to false in your theme to use a Toolbar instead.");        }        // If we reach here then we're setting a new action bar        // First clear out the MenuInflater to make sure that it is valid for the new Action Bar        mMenuInflater = null;        // If we have an action bar currently, destroy it        if (ab != null) {            ab.onDestroy();        }        if (toolbar != null) {            final ToolbarActionBar tbab = new ToolbarActionBar(toolbar,                    ((Activity) mContext).getTitle(), mAppCompatWindowCallback);            mActionBar = tbab;            mWindow.setCallback(tbab.getWrappedWindowCallback());        } else {            mActionBar = null;            // Re-set the original window callback since we may have already set a Toolbar wrapper            mWindow.setCallback(mAppCompatWindowCallback);        }        invalidateOptionsMenu();    }

主要在这里:

//这里会去判有没有actionbar存在,如果有直接抛异常final ActionBar ab = getSupportActionBar();        if (ab instanceof WindowDecorActionBar) {            throw new IllegalStateException("This Activity already has an action bar supplied " +                    "by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set " +                    "windowActionBar to false in your theme to use a Toolbar instead.");        }

好了,结束。

更多相关文章

  1. 【Android】下载Android源码的简明步骤
  2. android源码下载与编译过程记录
  3. Ubuntu 12.04(64位)下载并编译 Android(安卓)4.1 源码[只有2条命令
  4. android Error:Execution failed for task ':app:transformClass
  5. 使用RecyclerView加载不出数据的原因可能有:
  6. Android:Textview 通过代码设置 Drawable
  7. android控件之spinner (下拉列表)
  8. android4.2 屏蔽home键
  9. Android(安卓)debug 问题集合

随机推荐

  1. sdut 2162:The Android University ACM T
  2. Android(安卓)Studio开发之 JNI 篇的简单
  3. android设置Webview的滚动条属性
  4. Android(安卓)自己画View设置画笔的颜色
  5. android 导出jar包并使用
  6. Android中intent如何传递自定义数据类型
  7. Android开发_精准排布控件位置
  8. Android 图片Bitmap的剪切
  9. Debug Android and Linux suspend and re
  10. android webview里设置cookie