0

DrawerActivity.java

public class DrawerActivity extends MainActivity{
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
        private ActionBarDrawerToggle mDrawerToggle;

        private CharSequence mDrawerTitle;
        private CharSequence mTitle;
        private String[] navMenuItems;



        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
            super.onCreate(savedInstanceState);

            setContentView(R.layout.nav_drawer_menu);

            mTitle = mDrawerTitle = getTitle();
            navMenuItems = getResources().getStringArray(R.array.nav_drawer_items_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's list view with items and click listener
            mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, navMenuItems));
            //mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

            // enable ActionBar app icon to behave as action to toggle nav drawer
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().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) {
                    getSupportActionBar().setTitle(R.string.app_name);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

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


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // The action bar home/up action should open or close the drawer.
            // ActionBarDrawerToggle will take care of this.
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }


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

HomePage.java

  public class HomePage extends DrawerActivity {

        public static boolean active = false;
        private Connection con;

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

            /*
            try {
                con = new ConnectDataBase().getCon();
                if (con == null)
                    Toast.makeText(getApplicationContext(), "con is null", Toast.LENGTH_LONG).show();
                con.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            */

        }

When my main activity in android manifest is DrawerActivity, the drawer comes out when the app icon is pressed. but it doesn't come out in HomePage activity and there is no error. how to fix?!

Jerryc
  • 302
  • 2
  • 5
  • 20
  • For DrawerActivity your given one layout like(with menu) and in HomeActivity your given different layout(without menu),then how it will display menu for both activities,so based on the layout it will display menu – Hanuman Apr 06 '15 at 12:11
  • Can i know why you have extend MainActivity in your DrawerActivity? or what code you have done in MainActivity? –  Apr 06 '15 at 12:28
  • @AndroidWeblineindia just about the action bar and now i have put them together – Jerryc Apr 06 '15 at 13:44
  • @Allu then how should I do to apply the drawer code to all activities.. I just followed what people said – Jerryc Apr 06 '15 at 13:48
  • What i did in my application is i had created one activity like(DrawerActivity) and inside that i have used all(max) as fragments of DrawerActivity if we changed into activity then it has it's own layout.I did like that. – Hanuman Apr 06 '15 at 13:53
  • 1
    meaning if I want to do with activities, need to create drawer layout every time? @Allu – Jerryc Apr 06 '15 at 14:02
  • I think so,i don't know much about,i have used fragments see this links you can find better solution http://stackoverflow.com/questions/19451715/same-navigation-drawer-in-different-activities http://stackoverflow.com/questions/18697966/android-navigation-drawer-on-multiple-activities – Hanuman Apr 06 '15 at 14:08

1 Answers1

1

Its because you have used ActionBarDrawerToggle and mDrawerList as private in the super class, hence they're not inherited by your subclass! Make them public , and then try.

EDIT

use this line in your subclass

super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
super.onCreateDrawer(); // this line

See if this works

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41