MaterialDrawer在构建

时间:2017-08-15 16:48:32

标签: android android-studio materialdrawer

所以我尝试为我的Android应用程序构建MaterialDrawer侧边栏。但是,当抽屉打出来构建我的应用程序崩溃..任何想法为什么? 我的崩溃日志如下所示:

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

我不明白该活动应该通过哪里,事实并非如此。我提到我在一个类中构建我的MaterialDrawer侧边栏,我将用它作为多个活动的父级。所以这个课程的目的只是为我的应用程序生成这个侧面菜单。 我的代码:

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();
    }

谢谢大家的时间!

1 个答案:

答案 0 :(得分:1)

您需要在您的流利构建器中使用活动。

aka - 您忘了指定.withActivity(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();