2

i have a piece of code that is supposed to show navigation drawer in all activities. the code does not show any errors but while running it i get unfortunately stopped error after checking the logcat it shows java null pointer exception.

These has been declared

   private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    protected RelativeLayout _completeLayout, _activityLayout;

    private CharSequence mDrawerTitle;


    private CharSequence mTitle;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NaviDrawerListAdapter adapter;

After onCreate

    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 NaviDrawerListAdapter(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.ic_launcher, // 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);
    }
    @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();
    }

the null pointer exception points at mDrawerToogle.syncState(); in onPostCreate

ArK
  • 20,698
  • 67
  • 109
  • 136
Rahul
  • 45
  • 9
  • 3
    in each of your activity declare a navigation drawer, but i think its not required you can have 1 drawer and multiple fragments – Aniruddha K.M Jul 08 '15 at 04:40

4 Answers4

3

You don't need to have different navigation drawers for different Activities. Have a single Activity with NavigationDrawer and make all others as fragments. Have a look into this.

1

I recommend using this library, it is easy to "install" a nav drawer on an activity.

https://github.com/mikepenz/MaterialDrawer

new DrawerBuilder().withActivity(this).build()
Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
0

I really liked this approach : http://androiddeveloperdemo.blogspot.com.au/2014/08/android-navigation-drawer-with-multiple.html

Basically you create a BaseActivity class which extends Activity (normal) and in which you implement your Drawer. Then all other classes extend BaseActivity instead of Activity.

You still have to add the DrawerLayout in each of the XML files for all activities though.

JDenais
  • 2,956
  • 2
  • 21
  • 30
  • 1
    But, what is the need? Purpose of having a navigation drawer is to use a single activity with multiple fragments. – Seshu Vinay Jul 08 '15 at 05:03
  • 1
    @SeshuVinay I disagree with this statement. In my opinion, the Navigation Drawer is merely a UX component and should not have anything to see with the coding choices made. I don't really like the "One activity for many Fragment" pattern as it makes the code un-clear, but still want to use the Navigation Drawer. As stated in the google doc : "What you do in the onItemClick() method depends on how you've implemented your app structure" – JDenais Jul 08 '15 at 05:10
  • well, is it ideal to write same code again and again when you can do it in writing once? – Seshu Vinay Jul 08 '15 at 05:13
  • @SeshuVinay That is the whole point in this approach. You code the drawer once and then use inheritance to have it in all Activities. I will give you that you have to reproduce the drawer in XML, but it is really not much. – JDenais Jul 08 '15 at 05:17
  • Isn't it very difficult to manage navigation? You would have to finish() all non visible activities, and make the same control logic for navigation as you would do with fragments. And with very heavy graphics in the screens, All those sub activities in the memory, would make the app run out of memory all the time. – Seshu Vinay Jul 08 '15 at 05:40
  • @SeshuVinay lol you are quoting the SO question on app structure. http://stackoverflow.com/questions/12154505/one-activity-and-all-other-fragments. That is a good point if your app has HEAVY graphics. Mines usually don't. – JDenais Jul 08 '15 at 06:01
  • Yeah. I came across the same question while searching for pros and cons of using a single activity with multiple fragments. – Seshu Vinay Jul 08 '15 at 06:08
0

If you really want use navigation drawer in all activities not fragments this could be help to you

make a one activity with navigation drawer and extend all other activities with that activity

add this code in your navigation drawer activity and override this in your activity with your activity layout

protected int getlayout()
{
    return R.layout.navigation_drawer;
}

public String getTitleA()
{
    return "";
}
MaxExplode
  • 1,977
  • 2
  • 17
  • 27