支持Android v23.1.0更新中断NavigationView获得/找到头 [英] Android support v23.1.0 update breaks NavigationView get/find header

本文介绍了支持Android v23.1.0更新中断NavigationView获得/找到头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用的 v23.0.1 支持库到现在为止,没有任何问题。现在,当我切换到新的 v23.1.0 库我收到的部件一个空指针在抽屉里的布局。

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

活动的布局

<?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>

只需更改摇篮文件,以使用旧版本使得它做工精细瞬间,所以我不认为这有什么可怕的错误与我的code。我检查了修订的在更新,并没有看到任何东西,我会认为导致此。

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?

推荐答案

随着设计库 v 23.1.0 NavigationView 作品使用 RecyclerView
另外,标题现在是一种排。

With the design library v 23.1.0 the NavigationView works with a RecyclerView.
Also the Header is now a type of row.

这意味着该头的不能立即在视图层次可用。
它可以,如果你使用的是像 navigationView.findViewById方法造成的问题(XXX)获得头内部的视图。

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.

有一个在谷歌跟踪的错误。

修改2015年12月10号:设计库23.1.1

本23.1.1引入了一个新的API用于检索标题视图NavigationView与<一个href="http://developer.android.com/reference/android/support/design/widget/NavigationView.html#getHeaderView(int)">getHeaderView()

The 23.1.1 introduces a new API for retrieving header views for NavigationView with getHeaderView()

在23.1.1

解决方法FOT 23.1.0 可以使用 addOnLayoutChangeListener 。 Somenthing这样的:

workaround fot 23.1.0 can be to use a addOnLayoutChangeListener. Somenthing like:

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

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

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

  • 删除应用程序:headerLayout从XML 属性,然后编程方式添加的头

  • remove the app:headerLayout attribute from the xml, and then add the header programatically.

编程膨胀headerView。

Inflate the headerView programmatically.

使用somenthing是这样的:

Use somenthing like this:

View headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
headerLayout.findViewById(xxx);

这篇关于支持Android v23.1.0更新中断NavigationView获得/找到头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆