简体   繁体   English

Android支持v23.1.0更新中断NavigationView get / find标头

[英]Android support v23.1.0 update breaks NavigationView get/find header

I have been using the v23.0.1 support library until now with no problems. 到目前为止,我一直在使用v23.0.1支持库,没有任何问题。 Now when I switch to the new v23.1.0 library I am getting a null pointer on widgets in the drawer layout. 现在当我切换到新的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());

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 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. 只需更改gradle文件以使用旧版本即可立即正常工作,因此我认为我的代码没有任何可怕的错误。 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? 当然这也会影响其他人,任何线索?

With the design library v 23.1.0 the NavigationView works with a RecyclerView . 使用设计库v 23.1.0NavigationView可与RecyclerView配合使用。
Also the Header is now a type of row. 此外, Header现在是一种行。

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. 如果您使用navigationView.findViewById(XXX)等方法获取标题内的视图,则可能会导致问题。

There is a bug in the Google Tracker . Google Tracker中存在错误。

EDIT 12/10/2015: Design library 23.1.1 编辑12/10/2015:设计库23.1.1

The 23.1.1 introduces a new API for retrieving header views for NavigationView with getHeaderView() 23.1.1引入了一个新的API,用于使用getHeaderView()检索NavigationView的标题视图

BEFORE 23.1.1 在23.1.1之前

workaround fot 23.1.0 can be to use a addOnLayoutChangeListener . 变通方法fot 23.1.0可以使用addOnLayoutChangeListener Somenthing like: Somenthing喜欢:

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

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

Another possible workaround are: 另一种可能的解决方法是:

  • remove the app:headerLayout attribute from the xml, and then add the header programatically. 从xml中删除app:headerLayout属性,然后以编程方式添加标题。

  • Inflate the headerView programmatically. 以编程方式膨胀headerView。

Use somenthing like this: 像这样使用somenthing:

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

It appears attaching the header view to the navigation drawer using xml is currently broken. 似乎使用xml将标题视图附加到导航抽屉当前已损坏。 The solution is to inflate and attach the view manually. 解决方案是手动充气并附加视图。

activity layout 活动布局

<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" />

Then in your code inflate and attach the header by doing the following. 然后在您的代码中膨胀并通过执行以下操作附加标头。

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

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

I have updated build tools from Android sdk manager, then 23.1.0 is also working fine for me. 我已经从Android sdk管理器更新了构建工具,然后23.1.0也适合我。

I am using 我在用

buildToolsVersion "23.0.2"

before this it was 23.0.1. 在此之前它是23.0.1。

and there is no need of using 而且没有必要使用

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

In your activity you can directly use 在您的活动中,您可以直接使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM