此活动已经有一个由窗口装饰提供的操作栏?

时间:2016-01-17 17:06:47

标签: java android android-theme android-toolbar

我正在使用Android使用Toolbar创建一个Custom style应用,但是当我正在运行该应用时,我收到以下错误:

  

引起:java.lang.IllegalStateException:此Activity已经有   窗户装饰提供的动作栏。不要求   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);
    }
}

}

3 个答案:

答案 0 :(得分:12)

显然错误说:

  

此活动已有窗口装饰提供的操作栏。   请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并进行设置   主题中的 windowActionBar为false ,而不是使用工具栏

在您的样式中使用此功能:

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

<强>更新

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>

在您的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>

确保Toolbar正确实施。

例如,使用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" />

答案 1 :(得分:0)

只需转到清单文件,然后在特定活动中添加 android:theme =“ @ style / AppTheme.NoActionBar” />

答案 2 :(得分:0)

在我的情况下,这是因为手机处于暗模式,而我可能没有为我的应用提供暗模式。禁用黑暗模式后,它可以正常工作。