我一直在使用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 文件以使用旧版本即可使其立即正常工作,因此我认为我的代码没有任何严重错误。我查看了修订 https://developer.android.com/intl/ko/tools/support-library/index.html在更新中,没有看到任何我认为会导致此问题的内容。
这肯定也会影响其他人,有什么线索吗?
与设计库v 23.1.0 the NavigationView
与一个RecyclerView
.
还有Header
现在是一种行类型。
这意味着标题无法立即在视图层次结构中可用.
如果您使用类似的方法,可能会导致问题navigationView.findViewById(XXX)
获取标题内部的视图。
有一个错误谷歌追踪器 https://code.google.com/p/android/issues/detail?id=190226.
编辑 12/10/2015:设计库 23.1.1
23.1.1 引入了一个新的 API,用于检索 NavigationView 的标头视图获取标题视图() http://developer.android.com/reference/android/support/design/widget/NavigationView.html#getHeaderView(int)
23.1.1 之前
23.1.0 的解决方法可以是使用addOnLayoutChangeListener
。就像是:
navigationView.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
{
@Override
public void onLayoutChange( ... )
{
navigationView.removeOnLayoutChangeListener( this );
View view = navigationView.findViewById( ... );
}
} );
另一种可能的解决方法是:
使用这样的东西:
View headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
headerLayout.findViewById(xxx);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)