Issue
I am using a navigation drawer in my main activity defined as
private DrawerLayout drawerlayout;
I am using it in main activity to open and close my nav fragment, but there are 3 specific buttons in nav fragment class where I would like to use the nav fragment functionality drawerlayout.closedrawer(r.id.drawer)
But everytime I define it again in nav fragment class and try to add it in the onclick for these buttons, upon clicking the buttons, the app crashes with a nullpointer exception.How do I go about the same?
Thanks! Here's the sample code from my app: public class navfragment extends fragment {
public interface OnCloseDrawerListener
{
void onCloseDrawer();
}
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.button_logout:
final DialogFragment dialog = new LogoutCancelSignoutDialogFragment(mLogoutListener);
dialog.show(getActivity().getSupportFragmentManager(), AbsBaseaActivity.TAG_LOGOUT_DIALOG);
BangoHelper.eventLogout();
((OnCloseDrawerListener)getActivity()).onCloseDrawer();
break;
}
}
In my class MainActivity:
public class MainActivity extends AbsBaseaActivity implements OnBackStackChangedListener {
private DrawerLayout drawerLayout;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//----------Code for Navigation Drawer setup
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
public void onCloseDrawer()
{
drawerLayout.closeDrawer(R.id.drawer);
}
Even after modifying the code as per the given suggestion it crashes when I click on the button defined onclick for the fragment class, any clue?
Solution
You could add a callback from your fragment to your activity to close the drawer..
Ex.
public class YourFragment extends Fragment
{
public void onClick(View v)
{
((OnCloseDrawerListener)getActivity()).onCloseDrawer();
}
public interface OnCloseDrawerListener
{
void onCloseDrawer();
}
}
Then in your activity you would need to implement this interface
public class MyActivity extends Activity implements OnCloseDrawerListener
{
@Override
public void onCloseDrawer()
{
// add whatever code you need to close the drawer
}
}
Obviously you can name this interface something that more accurately represents your needs and pass it whatever arguments you require
Answered By - dymmeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.