【问题标题】:This Activity already has an action bar supplied by the window decor?这个 Activity 已经有一个由窗口装饰提供的操作栏?
【发布时间】:2016-04-22 19:36:28
【问题描述】:

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

Caused by: java.lang.IllegalStateException: This Activity has already has 窗口装饰提供的操作栏。不要要求 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);
    }
}

}

【问题讨论】:

标签: java android android-theme android-toolbar


【解决方案1】:

显然错误说:

这个 Activity 已经有一个由窗口装饰提供的操作栏。 不要请求 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" />

【讨论】:

  • 我正在尝试此代码,但给出了相同的错误原因:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
  • @BharatSindhav - 在你的清单中使用这个:android:theme="@style/AppTheme.NoActionBar"
【解决方案2】:

只需转到您的清单文件并在特定活动中添加

android:theme="@style/AppTheme.NoActionBar"/>

【讨论】:

    【解决方案3】:

    在我的情况下,发生这种情况是因为手机处于暗模式,并且我可能没有为我的应用程序提供暗模式。禁用暗模式后它可以正常工作。

    【讨论】:

      【解决方案4】:

      在我的情况下,我是在深色主题的移动设备上进行测试,我得到了这个,以下代码为我完成了工作

          <style name="Theme.QrCodeScanner" 
        parent="Theme.MaterialComponents.DayNight.NoActionBar">
      

      在themes.xm 和themes.xml(night) 中设置此项 或者如果您希望它用于特定活动,请在您的清单中的特定活动中执行以下操作

      android:theme="@style/AppTheme.NoActionBar"/>
      

      【讨论】:

        猜你喜欢
        • 2016-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-03
        相关资源
        最近更新 更多