Bre*_*vyn 29 android android-support-library android-design-library android-support-design android-navigationview
到目前为止,我一直在使用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());
Run Code Online (Sandbox Code Playgroud)
活动布局
<?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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
只需更改gradle文件以使用旧版本即可立即正常工作,因此我认为我的代码没有任何可怕的错误.我查看了修订版更新中,没有看到任何我认为会导致这种情况的修改.
当然这也会影响其他人,任何线索?
Gab*_*tti 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.Somenthing喜欢:
navigationView.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
{
@Override
public void onLayoutChange( ... )
{
navigationView.removeOnLayoutChangeListener( this );
View view = navigationView.findViewById( ... );
}
} );
Run Code Online (Sandbox Code Playgroud)
另一种可能的解决方法是:
app:headerLayout从xml中删除该属性,然后以编程方式添加标头.
以编程方式膨胀headerView.
像这样使用somenthing:
View headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
headerLayout.findViewById(xxx);
Run Code Online (Sandbox Code Playgroud)
Bre*_*vyn 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" />
Run Code Online (Sandbox Code Playgroud)
然后在您的代码中膨胀并通过执行以下操作附加标头.
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);
Run Code Online (Sandbox Code Playgroud)