NulledPointer exception when changing from support-v4 to appcompat-v7

My app is using Theme.Holo.light using com.android.support:support-v4:21.0.3. Now I changed my app theme to Theme.Appcompat and changed gradle dependencies also from "com.android.support:support-v4:21.0.3" to "com.android.support:appcompat-v7:21.0.3". After these changes my app crashes whenever i run it. and getting the log

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to call virtual method 'void android.app.ActionBar.setHomeButtonEnabled (boolean)' on null object Help on com .viter.android.navigationdrawerexample.MainActivity.onCreate (MainActivity.java:99)

My function

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Code for Tabs
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    // Set up the action bar.
    final ActionBar actionBar = getActionBar();


    actionBar.setHomeButtonEnabled(false);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }


    // Navigation Drawer
    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

      

Please help me to solve this.

+3


source to share


3 answers


You should probably use getSupportActionBar()

instead getActionBar()

(and also extend ActionBarActivity

instead FragmentActivity

).



+1


source


With AppCompat v21 you can add a toolbar and use it as an ActionBar. It's not easy to do, and most of the code doesn't need to be changed. The main thing is to add the toolbar to the activity_main.xml file

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

      

And then use this to make your action bar



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

ActionBar actionBar = getSupportActionBar();

      

}

There may be several other things to update depending on the rest of your code. But that should make you most of the way. More details here: https://chris.banes.me/2014/10/17/appcompat-v21/

+1


source


Check that your

final ActionBar actionBar = getActionBar();

      

returns null for actionBar.

You are probably calling code in the wrong class.

0


source







All Articles