Click here to Skip to main content
14,580,625 members
Rate this:
Please Sign up or sign in to vote.
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

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;


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);
       }
   }

@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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100