【发布时间】:2015-10-16 01:55:03
【问题描述】:
到目前为止,我一直在使用 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>
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>
只需将 gradle 文件更改为使用旧版本即可使其立即正常工作,因此我认为我的代码没有任何可怕的错误。我查看了更新中的revisions,没有看到任何我认为会导致此问题的内容。
这肯定也会影响到其他人,有什么线索吗?
【问题讨论】:
-
您检查过导航视图中的子节点数吗?你可以使用 getChildCount()
标签: android android-support-library android-design-library android-navigationview android-support-design