0

I am extending my NavigationObject class to my other activity classes but the drawer would not come out when I click on the menu.

Here is my NavigationObject:

public class NavigationObject extends ActionBarActivity implements OnClickListener{
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
ListView listView;
List<RowItem> rowItems;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.navdrawer); 

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
    System.out.println("ERROR 1");
    addDrawerItems();
    setupDrawer();
  } 


public boolean onCreateOptionsMenu(Menu menu) {      
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);
  }


//junk
   public void addDrawerItems() {
   System.out.println("ERROR 2");
    String[] drawer_Array = { "User Profile", "Setting", "Contact us", "Help", "Logout" };
    String[] desc_Array = { "Private", "Change settings", "Ask us anything", "Simple Guide", "Logs out user" };
    Integer[] image_Array = { R.drawable.user, R.drawable.setting, R.drawable.contactme, R.drawable.help, R.drawable.logout };

    //mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawer_Array);
    //mDrawerList.setAdapter(mAdapter);
    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < drawer_Array.length; i++) {
        RowItem item = new RowItem(image_Array[i], drawer_Array[i], desc_Array[i]);
        rowItems.add(item);
    }
    System.out.println("ERROR 3");
    listView = (ListView) findViewById(R.id.navList);
    CustomDrawerBaseAdapter adapter = new CustomDrawerBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 0){
                Intent toProfile = new Intent(getApplicationContext(), userprofActivity.class);
                startActivity(toProfile);
                //Toast.makeText(getMsgActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();   
            }
            else if(position == 1){
                //Toast.makeText(getMsgActivity.this, "Time for an Wake!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 2){
                //Toast.makeText(getMsgActivity.this, "Time for an Eat!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 3){
                //Toast.makeText(getMsgActivity.this, "Time for an Dance!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 4){
                //Toast.makeText(getMsgActivity.this, "Time for an Sleep!", Toast.LENGTH_SHORT).show();
            }   
        }
    });

    System.out.println("ERROR 4");
  }

     public void setupDrawer() {
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setHomeButtonEnabled(true);
   System.out.println("ERROR 5");
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            // enabling action bar app icon and behaving it as toggle button

                public void onDrawerOpened(View drawerView) {
                    System.out.println("ERROR 5");
                    super.onDrawerOpened(drawerView);
                    listView.bringToFront();
                    mDrawerLayout.requestLayout();
                    getSupportActionBar().setTitle("User Menu Option");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                public void onDrawerClosed(View view) {
                    System.out.println("ERROR 6");
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
        };
        System.out.println("ERROR 6.5");
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

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

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        System.out.println("ERROR 7");
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(listView);
        menu.findItem(R.id.action_write).setVisible(!drawerOpen);
        menu.findItem(R.id.action_grabmsg).setVisible(!drawerOpen);
        menu.findItem(R.id.action_mydiary).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            System.out.println("landscape");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            System.out.println("portrait");
        }
    } 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //bundle = getIntent().getExtras();
        //String language_chosen = bundle.getString("lang_pick");

        if (id == R.id.action_write) {
            Intent writeNewMsg = new Intent(getApplicationContext(), writeMsgActivity.class);
            //writeNewMsg.putExtra("lang_pick", language_chosen);
            startActivity(writeNewMsg);
        }
        else if(id == R.id.action_grabmsg){
            Intent grabNewMsg = new Intent(getApplicationContext(), getMsgActivity.class);
            //grabNewMsg.putExtra("lang_pick", language_chosen);
            finish();
            startActivity(grabNewMsg);

        }
        else if(id == R.id.action_mydiary){

        }

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

My problem is here:

            // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
            return true;
        }

and here these methods were never called:

                    public void onDrawerOpened(View drawerView) {
                    System.out.println("ERROR 5.5");
                    super.onDrawerOpened(drawerView);
                    listView.bringToFront();
                    mDrawerLayout.requestLayout();
                    getSupportActionBar().setTitle("User Menu Option");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                public void onDrawerClosed(View view) {
                    System.out.println("ERROR 6");
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

Here is the xml for the drawer called navdrawer.xml of NavigationObject class:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/navList"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/> 

</android.support.v4.widget.DrawerLayout>

UPDATED - STILL NEED ASSISTANCE

Here is where I use the extend on my other classes (This class do not have the drawer xml within their xml because I am inheriting from NavigationObject which has its own xml called navdrawer):

public class MainActivity extends NavigationObject implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.get_msg);   

Problem: Everytime I click the option menu bar on the drawer it prints "8" from the NavigationObject class but it does nothing after that. There was no error in the code because all of the system out print I placed worked except for System.out.println("ERROR 5.5") and System.out.println("ERROR 6") which are used to open and close the drawer. The swipe does not work.

This was working when I shoved all of NavigationObject class inside my MainActivity activity class but now it's not working when I made the drawer as a separate class (NavigationObject class) for inheritance.

The menu bar is working fine. But the optionselected at "ERROR 8" does not return anything but still prints out so it's working but its doing nothing either.

Any help is very much appreciated, this is for my final year project and it's due very soon :) .

nothingness
  • 694
  • 3
  • 9
  • 25

1 Answers1

0

You set a different layout (R.layout.get_msg) in your derived class. Does this layout contain the NavigationDrawer? If not -- there's your bug.

Edit

Try this: Move the initialization of the NavigationDrawer to a separate method in your base activity like so:

public class NavigationObject extends ActionBarActivity ...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
}

protected void initNavigationDrawer() {
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
    System.out.println("ERROR 1");
    addDrawerItems();
    setupDrawer();
} 

And in your MainActivity call the init method in your onCreate() method, after setting the content view.

public class MainActivity extends NavigationObject ...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_msg); // <-- must contain drawer_layout
    initNavigationDrawer();
}
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • no I did not put it in. I was inheriting from NavigationObject to use the navdrawer.xml so this way does not work. So you are suggesting I have the xml of the drawer inside the get_msg.xml rather than its own xml (navdrawer) but I have already tried this and when I swipe it works but comes out plain white and the drawer on optionselect on the menu does not open the drawer. – nothingness Apr 07 '15 at 12:47
  • This is what you are suggesting I think, I set `setContentView(R.layout.get_msg);` inside the NavigationObject class instead of `setContentView(R.layout.navdrawer);`. I have also added the drawer from the navdrawer.xml into get_msg.xml. – nothingness Apr 07 '15 at 12:52
  • You have to understand that when you call setContentView in your derived class, you replace the layout you set before in your base class. I would suggest to have no setContentView() in your base class at all, but to have the DrawerLayout in all your deriving classes. In the deriving classes call super.onCreate() _after_ setContentView(), so there is actually a layout with the navigation drawer when the base class does the `findViewById(R.id.drawer_layout)` and also the base class can add the navigation items. – Ridcully Apr 07 '15 at 19:14
  • After removing the xml from my base class Error at `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` it doesn't tell me the error, but it happens when I change what you said" call super.onCreate() after setContentView()" : `setContentView(R.layout.get_msg); super.onCreate(savedInstanceState); Firebase.setAndroidContext(this);` and if i didn't change what you said and leave it like it was before (but with the removed xml in base class), it would give me an Error at `listView.setAdapter(adapter);` but without any details why the error occurred. – nothingness Apr 07 '15 at 20:09