Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close drawer on item click #48

Closed
virajt71 opened this issue Apr 25, 2017 · 4 comments
Closed

close drawer on item click #48

virajt71 opened this issue Apr 25, 2017 · 4 comments

Comments

@virajt71
Copy link

virajt71 commented Apr 25, 2017

Hi...
I am trying to close drawer automatically when any item clicked in drawer. Have set new intent on item click and it work fine. But when i add code for close drawer on click of new intent the App crashes.

@mxn21
Copy link
Owner

mxn21 commented Apr 26, 2017

Can you give me the log for the App crash?thanks

@virajt71
Copy link
Author

Here is the code:
`public class MenuListFragment extends Fragment {

ImageView ivMenuUserProfilePhoto;
DrawerLayout mDrawerLayout;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container,
            false);
    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    ivMenuUserProfilePhoto = (ImageView) view.findViewById(R.id.ivMenuUserProfilePhoto);
    NavigationView vNavigation = (NavigationView) view.findViewById(R.id.vNavigation);
    vNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Toast.makeText(getActivity(),menuItem.getTitle(),Toast.LENGTH_SHORT).show();
            int id = menuItem.getItemId();
            Intent i = null;
            if (id == R.id.menu_settings){
                i = new Intent(getActivity().getApplicationContext(), SettingActivity.class);
            }
            startActivity(i);
            mDrawerLayout.closeDrawer(GravityCompat.START);
            return true;
        }
    });
    setupHeader();
    return  view ;
}

private void setupHeader() {
    int avatarSize = getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
    String profilePhoto = getResources().getString(R.string.user_profile_photo);
    Picasso.with(getActivity())
            .load(profilePhoto)
            .placeholder(R.drawable.img_circle_placeholder)
            .resize(avatarSize, avatarSize)
            .centerCrop()
            .transform(new CircleTransformation());
}

}`

log :
04-26 10:13:45.348 16915-16915/com.example.soulreaper.automation E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.closeDrawer(int)' on a null object reference
at com.example.soulreaper.automation.activity.MenuListFragment$1.onNavigationItemSelected(MenuListFragment.java:52)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:156)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

@mxn21
Copy link
Owner

mxn21 commented May 2, 2017

you can't get mDrawerLayout object in MenuListFragment, because it's in MainActivity's layout .
You can define a method in MainActivity:
public void closeMenu() {
if (mDrawer.isMenuVisible()) {
mDrawer.closeMenu();
}
}

and in MenuListFragment use : ((MainActivity)getActivity()).closeMenu() ;

@mxn21 mxn21 closed this as completed May 2, 2017
@virajt71
Copy link
Author

virajt71 commented May 4, 2017

I did try this method but maybe i have put some thing else instead of
"((MainActivity)getActivity()).closeMenu() ;" so, it did not work. Thanks for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants