Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am finding difficulty in implementing navigation drawer in all activities. i have created a base activity called MainActivity which has all the features of navigation drawer.in all my activity i extend mainactivity. now when i run the app i get unfortunately stopped in logcat the error points to "Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference
at com.example.rahulrk.navigationdrawer.MainActivity.onPostCreate(MainActivity.java:194)"
on that particular line i have "mDrawerToggle.syncState();"

this is the code for baseactivity
declaration part

Java
private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    protected RelativeLayout _completeLayout, _activityLayout;
    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;


Java
public void set(String[] navMenuTitles,TypedArray navMenuIcons) {
       mTitle = mDrawerTitle = getTitle();

       mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
       mDrawerList = (ListView) findViewById(R.id.left_drawer);

       navDrawerItems = new ArrayList<NavDrawerItem>();

       // adding nav drawer items
       if(navMenuIcons==null)
       {
           for(int i=0;i<navMenuTitles.length;i++)
           {
               navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
           }
       }else
       {
           for(int i=0;i<navMenuTitles.length;i++)
           {
               navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],navMenuIcons.getResourceId(i, -1)));
           }
       }

       mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

       // setting the nav drawer list adapter
       adapter = new NavDrawerListAdapter(getApplicationContext(),
               navDrawerItems);
       mDrawerList.setAdapter(adapter);

       // enabling action bar app icon and behaving it as toggle button
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);
       getSupportActionBar().setHomeButtonEnabled(true);
       // getSupportActionBar().setIcon(R.drawable.ic_drawer);

       mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
               R.drawable.android, // nav menu toggle icon
               R.string.app_name, // nav drawer open - description for
               // accessibility
               R.string.app_name // nav drawer close - description for
               // accessibility
       ) {
           public void onDrawerClosed(View view)
           {
               getSupportActionBar().setTitle(mTitle);
               // calling onPrepareOptionsMenu() to show action bar icons
               supportInvalidateOptionsMenu();
           }

           public void onDrawerOpened(View drawerView)
           {
               getSupportActionBar().setTitle(mDrawerTitle);
               // calling onPrepareOptionsMenu() to hide action bar icons
               supportInvalidateOptionsMenu();
           }
       };
       mDrawerLayout.setDrawerListener(mDrawerToggle);

   }
   private class SlideMenuClickListener implements
           ListView.OnItemClickListener
   {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position,
                               long id) {
           // display view for selected nav drawer item
           displayView(position);
       }
   }

Java
@Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        // if nav drawer is opened, hide the action items
        // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position)
    {
        // update the main content by replacing fragments
        switch (position)

        {
            case 0:
                Intent intent = new Intent(this, First.class);
                startActivity(intent);
                finish();
                break;
            case 1:
                Intent intent1 = new Intent(this, Second.class);
                startActivity(intent1);
                finish();
                break;

            default:
                break;
        }

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

where could i have made a mistake. please i need guidence
Posted
Comments
Richard MacCutchan 9-Jul-15 11:43am    
Look at the point where the failure occurs in your debugger, and check to see which variable is null.
Rahul Ramakrishnan 10-Jul-15 0:13am    
in onPostCreate() this code mDrawerToggle.syncState(); this is where i get null pointer exception. when i comment that line the app runs but the navigation drawer does not show any items
David Crow 22-Apr-20 8:51am    
"in all my activity i extend mainactivity."

This is not the way to achieve what you are after. See here and here.
OriginalGriff 30-Jan-22 11:43am    
Erm ... did you look at the date?

I suspect it was a spammer "solution" brought it back to the head of the queue.
David Crow 9-Feb-22 10:23am    
On an unrelated note...

What is the name of that HDD cloner software that you routinely recommend?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900