2017-08-15 88 views
1

所以我試着爲我的Android應用程序構建一個MaterialDrawer側欄。但是,當抽屜命中建立我的應用程序崩潰..一切想法爲什麼? 我崩潰日誌是這樣的:構建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

您需要在流利的構建器中使用Activity。您忘記指定.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();