You must pass in a non null View

10,815

Solution 1

It would seem that there is an issue regarding the navigation view. NavigationView findViewById can't find header view

The current fix/workaround is to find the header layout using.

final NavigationView mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    final View headerLayout = mNavigationView.inflateHeaderView(R.layout.header);

Once that is done you can refer to each element in the header layout like so.

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

Solution 2

Jude answer is correct; but if you do not want to inflate the header view again, try this version:

final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
final View header = navigationView.getHeaderView(0);
final ImageView imageView = (ImageView) header.findViewById(R.id.image);
Share:
10,815
Jude Fernandes
Author by

Jude Fernandes

I am a partner and one of the co-founders of Octalogic Tech located in Goa. We craft beautifully designed softwares and websites along with mobile apps.

Updated on June 13, 2022

Comments

  • Jude Fernandes
    Jude Fernandes almost 2 years

    I recently updated my project to the Android Support Library 23.1 and this part of my code now gives an error.

    It was working before the update and commenting just this part out allows me to run the app fine.What exactly is wrong or has change?

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

    This is the error log.

        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) 
    

    I am using a navigation drawer layout which contains a navigation view.This navigation view contains the header layout which i display and as it turns out the code which i commented out is used to display pictures for the header layout.So has anything changed in the new library update?

    • Vaishak Nair
      Vaishak Nair over 8 years
      Check and make sure that 'back' and 'profile' are non-null.
    • Simon
      Simon over 8 years
      please check line 74 of your mainActivity as that is where your error lays.
    • Jude Fernandes
      Jude Fernandes over 8 years
      The exact same code works before the 23.1.0 update,i reverted and checked too.It would seem that there is a problem with the app:headerLayout feature of the navigation view not finding the header layout anymore.
  • Melebius
    Melebius about 6 years
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.