到目前为止,我一直在使用v23.0.1支持库,没有任何问题。现在,当我切换到新的v23.1.0库时,我在抽屉布局中的小部件上获得了一个空指针。
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());
活动布局
<?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>
抽屉头.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>
只需更改 gradle 文件以使用旧版本即可使其立即正常工作,因此我认为我的代码没有任何可怕的错误。我检查了更新中的修订,没有看到任何我认为会导致这种情况的东西。
当然这也会影响其他人,有什么线索吗?