getActionBar()在FragmentActivity中返回null

时间:2016-02-05 18:37:12

标签: android android-actionbar android-tabs

我正在尝试使用this tutorial使用可滑动视图构建Android标签布局。

我的FragmentActivity是:

public class BookDetailsActivity extends FragmentActivity implements ActionBar.TabListener {
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = {"Description", "Find book"};
    static ArrayList<String> book;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_books_details);

        book = new ArrayList<String>();
        book = (ArrayList<String>) getIntent().getSerializableExtra("bookMap");

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), book, getApplicationContext());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    public static ArrayList<String> getRequiredData() {
        return book;
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

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

}

但是我收到了错误:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

错误说明:

Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference

为什么我会收到此错误?

3 个答案:

答案 0 :(得分:1)

我同意Pooya。 getSupportActionBar()应该有效

答案 1 :(得分:1)

getActionBar()更改为getSupportActionBar()

还将FragmentActivity更改为AppcompatActivity

答案 2 :(得分:1)

<强> Android Studio: ActionBar.TabListener does not work with AppCompatActivity?

ActionBar.TabListener已被弃用interface。我希望您将来不再使用它。

并且ActionBarTabs可以替换为:

  

就像我之前在答案中所说:https://stackoverflow.com/a/35203962/4409113

但是为了解决问题,那是因为ActionBar 没有为正确的主题启用,而您正在扩展FragmentActivity

请检查此链接:Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

使用AppCompatActivity来添加ActionBar,如果您使用它,您也需要这样:

ActionBar actionBar = getSupportActionBar();