Java 此活动已具有由window decor提供的操作栏?
我正在使用Java 此活动已具有由window decor提供的操作栏?,java,android,android-theme,android-toolbar,Java,Android,Android Theme,Android Toolbar,我正在使用工具栏和自定义样式制作一个Android应用程序,但是当我运行应用程序时,我得到以下错误: 原因:java.lang.IllegalStateException:此活动已发生 由窗户装饰提供的动作条。不要求 Window.FEATURE\支持\操作\栏并在中将windowActionBar设置为false 您的主题将改为使用工具栏 Styles.xml <resources> <!-- Base application theme. --> <style
工具栏
和自定义样式
制作一个Android
应用程序,但是当我运行应用程序时,我得到以下错误:
原因:java.lang.IllegalStateException:此活动已发生
由窗户装饰提供的动作条。不要求
Window.FEATURE\支持\操作\栏并在中将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>
@颜色/原色
@颜色/原色暗
@颜色/原色
@颜色/文本颜色主
@颜色/文本颜色主
@颜色/原色
@颜色/原色暗
@颜色/原色
@颜色/原色
@颜色/原色
@颜色/彩色背景
大胆的
@颜色/原色
@颜色/原色
@颜色/原色暗
@颜色/原色
@颜色/彩色背景
假的
大胆的
main活动:
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);
}
}
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<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>
public类MainActivity扩展了AppCompatActivity{
私人摊位摊位;
私人视页机视页机;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content\u information\u category1);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar\u信息);
设置支持操作栏(工具栏);
viewPager=(viewPager)findViewById(R.id.viewPager);
设置viewPager(viewPager);
tabLayout=(tabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
专用无效设置ViewPager(ViewPager ViewPager){
ViewPagerAdapter适配器=新的ViewPagerAdapter(getSupportFragmentManager());
addFragment(新的TodayInformationFragment(),“Today”);
addFragment(新ThisWeekInformationFragment(),“本周”);
addFragment(新ThisMonthInformationFragment(),“本月”);
addFragment(新的AllyerInformationFragment(),“All”);
viewPager.setAdapter(适配器);
}
类ViewPagerAdapter扩展了FragmentPagerAdapter{
私有最终列表MFFragmentList=new ArrayList();
私有最终列表MFFragmentTitleList=new ArrayList();
公共视图页面编辑器(碎片管理器){
高级经理;
}
@凌驾
公共片段getItem(int位置){
返回MFFragmentList.get(位置);
}
@凌驾
public int getCount(){
返回MFFragmentList.size();
}
public void addFragment(片段片段,字符串标题){
添加(片段);
MFFragmentTitleList.add(标题);
}
@凌驾
公共字符序列getPageTitle(int位置){
返回MFFragmentTitleList.get(位置);
}
}
}错误清楚地表明:
此活动已具有由window decor提供的操作栏。
不要请求Window.FEATURE\u SUPPORT\u ACTION\u栏并设置
将主题中的windowActionBar设置为false,以改用工具栏
在您的样式中使用此选项:
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);
}
}
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<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>
并在您的清单中
:
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);
}
}
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<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>
只需转到清单文件,在特定的活动中添加
android:theme=“@style/AppTheme.NoActionBar”/>在我的例子中,发生这种情况是因为手机处于黑暗模式,而我没有为我的应用程序提供黑暗模式。禁用暗模式后,它可以正常工作。我正在尝试此代码,但给出了由以下原因引起的相同错误:java.lang.IllegalStateException:此活动已具有由window decor提供的操作栏。不要请求Window.FEATURE\u SUPPORT\u ACTION\u BAR,并将主题中的windowActionBar设置为false以使用工具栏。@BharatSindhav-在清单中使用此选项:
android:theme=“@style/AppTheme.NoActionBar”
主题的可能重复项看起来很好,您在清单中指向它了吗?