Android Error [Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference]

后端 未结 24 2008
误落风尘
误落风尘 2020-11-28 22:13

I have a code module which implements viewpager with navigation drawer, however, when I run the code I get the following error

01-26 09:20:02.958: D/AndroidR         


        
24条回答
  •  盖世英雄少女心
    2020-11-28 23:03

    Your code is throwing on com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95):

            // enabling action bar app icon and behaving it as toggle button
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);
    

    The problem is pretty simple- your Activity is inheriting from the new android.support.v7.app.ActionBarActivity. You should be using a call to getSupportActionBar() instead of getActionBar().

    If you look above around line 65 of your code you'll see that you're already doing that:

            actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            // TODO: Remove the redundant calls to getSupportActionBar()
            //       and use variable actionBar instead
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
    

    And then lower down around line 87 it looks like you figured out the same:

            getSupportActionBar().setTitle(
                            Html.fromHtml("" + mTitle + " - "
                                            + menutitles[0] + ""));
            // getActionBar().setTitle(mTitle +menutitles[0]);
    

    Notice how you commented out getActionBar().

提交回复
热议问题