您必须传入非空视图

时间:2015-10-16 17:14:39

标签: android view navigation android-appcompat drawer

我最近将我的项目更新到了Android支持库23.1,现在这部分代码出错了。

它在更新之前工作并且评论只是这部分允许我运行应用程序罚款。究竟是错误还是有变化?

Glide.with(getApplicationContext())
            .load(R.drawable.banner)
            .fitCenter()
            .override(width, height / 2)
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .into(back);

    if (picture != null) {
        Glide.with(getApplicationContext())
                .load(picture)
                .fitCenter()
                .override(width / 2, height / 2)
                .into(profile);
    } else {
        Glide.with(getApplicationContext())
                .load(R.drawable.profile_p)
                .fitCenter()
                .override(width / 2, height / 2)
                .into(profile);
    }

这是错误日志。

    10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime: FATAL EXCEPTION: main
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime: Process: atsystems.cal, PID: 16313
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{atsystems.cal/atsystems.cal.MainActivity}: java.lang.IllegalArgumentException: You must pass in a non null View
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: You must pass in a non null View
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.bumptech.glide.GenericRequestBuilder.into(GenericRequestBuilder.java:678)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.bumptech.glide.DrawableRequestBuilder.into(DrawableRequestBuilder.java:448)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at atsystems.cal.MainActivity.onCreate(MainActivity.java:74)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
10-16 22:41:27.238 16313-16313/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我正在使用导航抽屉布局,其中包含导航视图。此导航视图包含我显示的标题布局,因为它显示我注释掉的代码用于显示标题布局的图片。所以有任何东西在新库更新中更改了吗?

2 个答案:

答案 0 :(得分:11)

似乎导航视图存在问题。 NavigationView findViewById can't find header view

目前的修复/解决方法是使用。

查找标题布局
final NavigationView mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    final View headerLayout = mNavigationView.inflateHeaderView(R.layout.header);

完成后,您可以像这样引用标题布局中的每个元素。

final ImageView profile = (ImageView) headerLayout.findViewById(R.id.profile_image);

答案 1 :(得分:0)

裘德的回答是正确的;但如果您不想再次膨胀标题视图,请尝试以下版本:

final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
final View header = navigationView.getHeaderView(0);
final ImageView imageView = (ImageView) header.findViewById(R.id.image);