在android中将actionbar移动到工具栏时发生java.lang.IllegalStateException

在android中将actionbar移动到工具栏时发生java.lang.IllegalStateException,java,android,android-actionbar,android-toolbar,Java,Android,Android Actionbar,Android Toolbar,我一直在尝试将应用程序中的操作栏移动到工具栏。我在运行时代码中的setSupportActionBar(工具栏)行出现以下错误 Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar

我一直在尝试将应用程序中的操作栏移动到工具栏。我在运行时代码中的
setSupportActionBar(工具栏)
行出现以下错误

Caused by: java.lang.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.
我浏览了许多关于同一问题的帖子,但似乎没有任何效果。下面是我的代码的详细信息

这是我关于创建函数的活动

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_layout);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

    <FrameLayout
        android:id="@+id/detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

toolbar.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/primary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
        <item name="colorAccent">@color/accent</item>
</style>
<style name="AppMainTheme" parent="Theme.AppCompat.Light.NoActionBar" />
<style name="UItheme" parent="AppTheme"/>

@颜色/原色
@颜色/原色暗
@颜色/口音
清单文件是这样的

<application
        android:theme="@style/AppMainTheme">
        <activity
           android:name="com.android.ui.activities.MainActivity"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            android:theme="@style/UItheme" >
        </activity>
</application>


有人能告诉我可能出现的问题吗?

您可以尝试以下步骤

  • 使用AppCompatActivity扩展您的活动
  • 注释掉行请求窗口功能
  • 在清单文件中声明活动的主题

     <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    
    
    
  • 在样式中定义以下代码段。xml
    
    假的
    真的
    


如果您使用的是
AppCompatActivity
(问题不清楚)。。。使用适当的支持方法请求窗口feature@Selvin是,我正在使用AppCompat活动。你所说的正确支撑方法是什么意思?检查documentation@Selvin我将requestWindowFeature更改为supportRequestWindowFeature,但问题仍然存在。我为什么要使用什么?你能说清楚一点吗。