androidandroid-studiomaterialdrawer

MaterialDrawer crash on build


So I tried to build a MaterialDrawer sidebar for my Android app. But when the drawer hits build my app crashes..any ideas why? My crash log looks like this:

Caused by: java.lang.RuntimeException: please pass an activity
at com.mikepenz.materialdrawer.DrawerBuilder.build(DrawerBuilder.java:1300)

I don't understand where that activity should be passed and it is not. I mention that I build my MaterialDrawer sidebar in a class that I will use as a parent of multiple activities. So the purpose of this class is solely for generating this side menu for my app. My code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sidebar_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        User u = MainProvider.sharedInstance().getCurrentUser(this);
        TextView usernameText = (TextView) headerView.findViewById(R.id.usernameText);
        String profilePictureUrl = u.getSettings().get("profile_picture").getAsString();
        //initialize and create the image loader logic
        DrawerImageLoader.init(new AbstractDrawerImageLoader() {
            @Override
            public void set(ImageView imageView, Uri uri, Drawable placeholder) {
                Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
            }

            @Override
            public void cancel(ImageView imageView) {
                Picasso.with(imageView.getContext()).cancelRequest(imageView);
            }
        });
        //if you want to update the items at a later time it is recommended to keep it in a variable
        PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.dashboard);
        SecondaryDrawerItem item2 = new SecondaryDrawerItem().withIdentifier(2).withName(R.string.point_of_sale);
        // Create the AccountHeader
        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.drawable.side_nav_bar)
                .addProfiles(
                        new ProfileDrawerItem().withName(u.getUsername()).withEmail(u.getEmail()).withIcon(Uri.parse(profilePictureUrl))
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();
        new DrawerBuilder()
                .withAccountHeader(headerResult)
                .withToolbar(toolbar)
                .addDrawerItems(
                        item1,
                        new DividerDrawerItem(),
                        item2,
                        new SecondaryDrawerItem().withName(R.string.point_of_sale)
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        return false;
                    }
                })
                .withSavedInstance(savedInstanceState)
                .build();
    }

Thank you all for your time!


Solution

  • You need to use an Activity in your fluent builder.

    aka - You forgot to specify .withActivity(this).

    Try this:

        new DrawerBuilder()
            .withActivity(this)
            .withAccountHeader(headerResult)
            .withToolbar(toolbar)
            .addDrawerItems(
                item1,
                new DividerDrawerItem(),
                item2,
                new SecondaryDrawerItem().withName(R.string.point_of_sale)
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener()
            {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem)
                {
                    // do something with the clicked item :D
                    return false;
                }
            })
            .withSavedInstance(savedInstanceState)
            .build();