【问题标题】:Getting error in Sliding Tab and AppCompatActiivty theme in android在 android 中的 Sliding Tab 和 AppCompatActiivty 主题中出现错误
【发布时间】:2015-07-10 16:43:26
【问题描述】:

我在我的应用的 First Activity 中使用带有一些操作栏按钮的 AppCompatActivity

来自第二个活动,我正在使用滑动标签

我的 theme.xml(主题 1)

<style name="CustomActionBarTheme" parent="Theme.AppCompat">

        <!-- theme customizations -->

        <item name="colorPrimary">@color/easy</item>
        <item name="colorAccent">@color/easy</item>


           <!-- Size Of Action bar-->
         <item name="actionBarSize">55dp</item>


    </style>

第一个活动工作正常。使用上述主题但是

我的滑动标签页出错

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidingtab}:
 java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. 
Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我的主题.xml(主题 2)

<style name="CustomActionBarTheme" parent="Theme.AppCompat">

</style>

<style name="MyTheme.NoActionBar">
    <!-- Both of these are needed -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

如果我使用上述主题,Sliding TAB 页面工作正常,但我的第一个活动显示 NPE。

问题:

1.我想要第一个活动的自定义主题,

2.我也需要在第二个活动中滑动标签

如何解决这个问题,请帮助我。

编辑:1

添加了 Oncreate 方法

我的滑动标签 Oncreate

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_stores_0_tabview__landing);

        // for ActionBar back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Initilization

        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles
        // fot the Tabs and Number Of Tabs.
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true,
                                        // This makes the tabs Space Evenly in
                                        // Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);



        // End OF OnCreate

    }

【问题讨论】:

  • 请提供您的 onCreate 方法的代码
  • 第一个活动 oncreate 或 Sliding tab?
  • 我在创建时添加了我的

标签: android android-theme pagerslidingtabstrip android-tablayout


【解决方案1】:

我建议你为他们定义不同的主题。

我认为问题不是由 Sliding TAB 引起的, 再次检查您的代码。

您的第一个 Activity 主题:

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

您的第二个活动主题:

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

在您的 AndroidManifest.xml 中

<activity android:name=".first Activity" android:theme="@style/AppTheme"/>
<activity android:name=".second Activity" android:theme="@style/AppTheme2"/>

【讨论】:

  • 是的,问题不在我的滑动标签中,我想为第一个活动分配主题,没有用于滑动标签的操作栏。如何做到这一点。
  • name="NoActionTheme" parent="Theme.AppCompat.Light.NoActionBar"
  • 我已经在我的问题中提到过,如果明确应用此代码,滑动选项卡将起作用,但是,我需要带有操作栏的第一个活动。
  • 在我的 Firstactivity 中我有菜单项,所以我需要 Actionbar。
  • 我不明白你为什么要使用单一主题来满足不同的需求。您可以为他们定义两个主题,并在 AndroidManifest.xml 中分配它
猜你喜欢
  • 2013-09-01
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多