【问题标题】:java.lang.NullPointerException on Actionbar Android操作栏 Android 上的 java.lang.NullPointerException
【发布时间】:2015-08-04 13:57:30
【问题描述】:

我收到以下错误:

错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)”。

我正在尝试创建一个带有片段的标签栏。


MainActivity.xml

    TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
    tab = (ViewPager)findViewById(R.id.pager);
    tab.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {

                    actionBar = getSupportActionBar();
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    tab.setAdapter(TabAdapter);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabUnselected(ActionBar.Tab tab2, FragmentTransaction ft) {
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab2, FragmentTransaction ft) {
            tab.setCurrentItem(tab2.getPosition());
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab2, FragmentTransaction ft) {
        }
    };
    actionBar.addTab(actionBar.newTab().setText("Credit card").setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Net banking").setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Debit Card").setTabListener(tabListener));

样式.xml

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/screen_background</item>
    <item name="android:windowBackground">@color/windowBackground</item>

</style>

<style name="menu_labels_style">
    <item name="android:background">@drawable/fab_label_background</item>
    <item name="android:textColor">@color/white</item>
</style>


<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColorPrimary">@color/list_view_blue</item>
    <item name="android:textColorSecondary">@color/list_view_blue</item>
</style>

activity_main.xml

<android.support.v4.widget.DrawerLayout        
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.omniware.tracknpay_material.activity.Payment">
<android.support.v4.view.ViewPager

android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.omniware3.MenuPage" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />



</LinearLayout>
</android.support.v4.widget.DrawerLayout>

工具栏.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:id="@+id/toolbar_title"
    android:textColor="#010101" />
<!--android:layout_gravity="center"-->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/walletAmount"
    android:textColor="#010101"
    android:layout_alignParentRight="true"
    android:layout_gravity="right"
    android:paddingRight="5dip"
    />



 </android.support.v7.widget.Toolbar>

【问题讨论】:

  • 你看你的主题了吗?你有Theme.AppCompat.NoActionBar
  • 我将它替换为 Light.DarkActionBar,但它给出了 java.lang.IllegalArgumentException: AppCompat does not support the current theme features 错误。

标签: android android-actionbar


【解决方案1】:

这是答案NoActionBar

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.NoActionBar">

【讨论】:

  • 我将它替换为 Light.DarkActionBar,但它给出了 java.lang.IllegalArgumentException: AppCompat does not support the current theme features 错误。
猜你喜欢
  • 2021-02-26
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多