I have been using the v23.0.1 support library until now with no problems. Now when I switch to the new v23.1.0 library I am getting a null pointer on widgets in the drawer layout.
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
TextView username = (TextView) mNavigationView.findViewById(R.id.username_textView);
// ^^^^^^^^ is now null when using new library
// which causes the following to fail
username.setText(mUser.getName());
activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
...
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_items" />
</android.support.v4.widget.DrawerLayout>
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<TextView
android:id="@+id/username_textView"
android:layout_width="match_parent"
android:layout_height="0dp" />
...
</LinearLayout>
Simply changing the gradle file to use the older version makes it work fine instantly so I don't think there is anything horribly wrong with my code. I checked out the revisions in the update and didn't see anything that I would think to cause this.
Surely this will be affecting others also, any clues?
I have updated build tools from Android sdk manager, then 23.1.0 is also working fine for me.
I am using
before this it was 23.0.1.
and there is no need of using
In your activity you can directly use
With the design library v 23.1.0 the
NavigationView
works with aRecyclerView
.Also the
Header
is now a type of row.It means that the header could not be immediately available in the view hierarchy.
It can cause issues if you are using methods like
navigationView.findViewById(XXX)
to get a view inside the header.There is a bug in the Google Tracker.
EDIT 12/10/2015: Design library 23.1.1
The 23.1.1 introduces a new API for retrieving header views for NavigationView with getHeaderView()
BEFORE 23.1.1
workaround fot 23.1.0 can be to use a
addOnLayoutChangeListener
. Somenthing like:Another possible workaround are:
remove the
app:headerLayout
attribute from the xml, and then add the header programatically.Inflate the headerView programmatically.
Use somenthing like this:
it is a bug at 23.1.0
23.1.1 fixed
https://plus.google.com/+AndroidDevelopers/posts/ebXLByBiEBU
in the new
NavigationView
the header is now a row type ofRecyclerView
in order for you or anybody to find theview
by itsid
you'll need to workaround it and useaddOnLayoutChangeListener
listener and then you can find theview
i know it should be documented somewhere but android be like meh!.It appears attaching the header view to the navigation drawer using xml is currently broken. The solution is to inflate and attach the view manually.
activity layout
Then in your code inflate and attach the header by doing the following.