How to open Navigation Drawer with no actionbar, open with just a button

百般思念 提交于 2019-11-27 11:07:34
athor

It's giving you a null pointer because you are trying to find the drawer layout in the fragment's view, when it is actually in the activities view.

A quick hack to do what you want is to find the view like:

getActivity().findViewById(R.id.drawer_layout)

That should work. A better way is to have a method on the activity for opening the drawer

public void openDrawer(){
    mDrawerLayout.openDrawer(mDrawerLayout);
}

In the activity onCreate run your findViewById:

mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

mDrawerLayout should be a member variable of your activity.

Then in your fragment you can call:

//cast activity to MyActivity so compiler doesn't complain
((MyActivity)getActivity()).openDrawer();

An even better way to do it is to create a listener in the fragment and set the activity as a listener to the fragment. Then you can call a method on the activity, similar to above. I'll let you do some research on how to do that.

Aliya
drawerLayout.openDrawer(Gravity.START);

Thanks to @athor & @Ashish Tana.

It took me so much time to figure out the error (NullPointerException) I am getting.

Mine works this way; Instead of getView(), I use getActivity() and open the drawer by mDrawerLayout.openDrawer(Gravity.LEFT);.

mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
final ImageButton btnOpenDrawer = (ImageButton) getView().findViewById(R.id.drawer_menu);

        btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });

I have a simpler solution which uses isDrawerOpen() of DrawerLayout.

The code below closes or opens the navigation drawer based on the drawer's current state (Opened/Closed)

Button hamMenu = findViewById(R.id.ham_menu);
hamMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
        // If navigation drawer is not open yet, open it else close it.
        if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
        else navDrawer.closeDrawer(Gravity.END);
    }
});
rahstame

I wrote an answer about this here: https://stackoverflow.com/a/18199771/880349

   //For me a better way in avoiding a `null pointer` in getting the DrawerLayout
   final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
   btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                            //Opens the Drawer
                drawer.openDrawer(Your View, Usually a ListView);
            }

                return false;
        });

You directly get the current drawer inside a view/fragement so that you won't get a NullPointerException

It works on Button click

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            drawer.openDrawer(GravityCompat.START);

        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!