29

到目前为止,我一直在使用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 文件以使用旧版本即可使其立即正常工作,因此我认为我的代码没有任何可怕的错误。我检查了更新中的修订,没有看到任何我认为会导致这种情况的东西。

当然这也会影响其他人,有什么线索吗?

4

5 回答 5

41

使用设计库v 23.1.0可以NavigationView使用RecyclerView.
现在也是Header一种行。

这意味着标题不能立即在视图层次结构中可用
如果您使用诸如navigationView.findViewById(XXX)在标题中获取视图之类的方法,则可能会导致问题。

Google Tracker中有一个错误。

编辑 12/10/2015:设计库 23.1.1

23.1.1 引入了一个新的 API,用于使用getHeaderView()检索 NavigationView 的标题视图

23.1.1 之前

解决方法 fot 23.1.0可以使用addOnLayoutChangeListener. 像这样的东西:

navigationView.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
{
    @Override
    public void onLayoutChange( ... )
    {
        navigationView.removeOnLayoutChangeListener( this );

        View view = navigationView.findViewById( ... );
    }
} );

另一种可能的解决方法是:

  • 从 xml中删除app:headerLayout属性,然后以编程方式添加标头。

  • 以编程方式膨胀 headerView。

使用这样的东西:

View headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
headerLayout.findViewById(xxx);
于 2015-10-16T05:42:54.553 回答
11

似乎使用 xml 将标题视图附加到导航抽屉目前已损坏。解决方案是手动膨胀和附加视图。

活动布局

<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" <!-- remove this line -->
    app:menu="@menu/drawer_items" />

然后在您的代码中膨胀并通过执行以下操作附加标题。

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View drawerHeader = navigationView.inflateHeaderView(R.layout.drawer_header);

TextView username = (TextView) drawerHeader.findViewById(R.id.username_textView);
于 2015-10-16T03:07:18.530 回答
0

我已经从 Android sdk manager 更新了构建工具,然后 23.1.0 对我来说也可以正常工作。

我在用

buildToolsVersion "23.0.2"

在此之前是 23.0.1。

并且不需要使用

(View)navigationView.findViewById(R.id.idOfViewFromHeaderView);

在您的活动中,您可以直接使用

(View)findViewById(R.id.idOfViewFromHeaderView);
于 2016-01-04T16:55:10.700 回答
0

在新NavigationView的标题中,现在是一个行类型,RecyclerView 以便您或任何人找到viewid,您需要解决它并使用addOnLayoutChangeListener侦听器,然后您可以找到view我知道它应该记录在某处但 android 就像 meh !.

于 2015-10-16T03:21:21.013 回答
0

这是 23.1.0 的错误

23.1.1 固定

https://plus.google.com/+AndroidDevelopers/posts/ebXLByBiEBU

于 2015-11-13T02:18:34.577 回答