java 这个 Activity 已经有一个由窗口装饰提供的操作栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34841055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:26:06  来源:igfitidea点击:

This Activity already has an action bar supplied by the window decor?

javaandroidandroid-themeandroid-toolbar

提问by Bharat Sindhav

I am making an Androidapp using Toolbarwith Custom stylebut when i'm running the app, i'm getting the following error:

我正在制作一个Android使用Toolbarwith的应用程序,Custom style但是当我运行该应用程序时,出现以下错误:

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.

引起:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。

Styles.xml

样式文件

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColor">@color/textColorPrimary</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="android:textColorSecondary">@color/textColorPrimary</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>

</style>


<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/colorBackground</item>
    <item name="android:textStyle">bold</item>
    <item name="android:endColor">@color/colorPrimary</item>




    <!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>

<style name="TabTextAppearance" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/colorBackground</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textStyle">bold</item>
</style>

MainActivity:

主要活动:

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_information_category1);
    Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar_information);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new TodayInformationFragment(), "Today");
    adapter.addFragment(new ThisWeekInformationFragment(), "This Week");
    adapter.addFragment(new ThisMonthInformationFragment(), "This Month");
    adapter.addFragment(new AllyearInformationFragment(), "All");
    viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

}

回答by ??????

Clearly error says:

明显错误说:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to falsein your theme to use a Toolbar instead

这个 Activity 已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置 为 false以使用工具栏

Use this in your Styles:

在您的样式中使用它:

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

Updated:

更新:

Android studio's default example is like this, take a look at the AppTheme.NoActionBar:

Android studio 的默认例子是这样的,看一下AppTheme.NoActionBar

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

And in your Manifest:

而在你的Manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

And make sure your Toolbarimplemented correctly.

并确保您Toolbar正确实施。

For example, with AppbarLayout:

例如,使用AppbarLayout

<android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarmain"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/ColorPrimary"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

回答by Martin Kinuthia

Just go to your Manifest file and in the specific activity add android:theme="@style/AppTheme.NoActionBar"/>

只需转到您的清单文件并在特定活动中添加 android:theme="@style/AppTheme.NoActionBar"/>