我使用的是mikepenz材质抽屉库,无法在材质抽屉中添加标题。以下是我的代码
在gradle中我使用了这种依赖
compile('com.mikepenz:materialdrawer:4.0.0@aar') {
transitive = true
}
然后在MainActivity.java中,我使用了相同的代码片段,如文档中所示。
AccountHeader headerresult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.sunlight_foundation)
.addProfiles(
new ProfileDrawerItem().withName("Random Name").withEmail("random@gmail.com").withIcon(getResources().getDrawable(R.drawable.sunlight_foundation)),
).withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile iProfile, boolean b) {
return false;
}
}).build();
//create the drawer and remember the `Drawer` result object
final Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerresult)
.withToolbar(toolbar)
.build();
我收到了这个错误。
Process: com.example.rajni.congress, PID: 19041
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajni.congress/com.example.rajni.congress.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mikepenz.materialdrawer.adapter.BaseDrawerAdapter.addHeaderDrawerItems(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mikepenz.materialdrawer.adapter.BaseDrawerAdapter.addHeaderDrawerItems(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem[])' on a null object reference
at com.mikepenz.materialdrawer.DrawerUtils.handleHeaderView(DrawerUtils.java:231)
at com.mikepenz.materialdrawer.DrawerBuilder.createContent(DrawerBuilder.java:1446)
at com.mikepenz.materialdrawer.DrawerBuilder.buildView(DrawerBuilder.java:1280)
at com.mikepenz.materialdrawer.DrawerBuilder.build(DrawerBuilder.java:1152)
at com.example.rajni.congress.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
任何帮助都将受到高度赞赏。提前谢谢。
答案 0 :(得分:0)
创建headerresult后尝试将配置文件设置为活动配置文件。
像这样:
headerresult.setActiveProfile(headerResult.getProfiles().get(0));