2016-03-08 49 views
2

我有一个DrawerLayout,它包含一个带有页眉布局的NavigationView。的XML如下:为什么findViewById()在抽屉头中为视图返回null?

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
             xmlns:app="http://schemas.android.com/apk/res-auto" 
             android:id="@+id/drawer_layout" 
             android:layout_width="match_parent" 
             android:layout_height="match_parent"> 

    <android.support.design.widget.NavigationView 
      android:id="@+id/navigation_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      app:headerLayout="@layout/drawer_header" 
      app:menu="@menu/drawer"/> 

</android.support.v4.widget.DrawerLayout> 

headerLayoutLinearLayout它封装了ImageView,下面是drawer_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@color/blue_primary" 
    android:padding="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="120dp" 
     android:id="@+id/ivDrawerUserFace" 
     android:layout_height="120dp" /> 
</LinearLayout> 

我不知道为什么我找不到ImageView通过findViewById(R.id.ivDrawerUserFace),这改为返回null。但是,我可以通过以下代码找到它:

ViewGroup headerView = (ViewGroup) mNavigationView.getHeaderView(0); 
ImageView faceView = (ImageView) headerView.getChildAt(0); // faceView is not null 
String faceUrl = QingUApp.getInstance().getFaceUrl(); 
if (faceUrl != null) { 
    Glide.with(this).load(faceUrl).into(faceView); 
} 
View viewById = findViewById(R.id.ivDrawerUserFace); // viewById is null 

我在上面的代码之前调用了setContentView()。并且Glide成功将图像加载到ImageView中。为什么我不能通过findViewById()找到它?

我想这是因为图像视图不在传递给setContentView()的XML文件中。不过,我能得到通过findViewById()的观点在同一Activity有这样的代码:

private LoaderManager.LoaderCallbacks<Bitmap> mUserFaceLoaderCallbacks = new LoaderManager.LoaderCallbacks<Bitmap>() { 
    @Override 
    public Loader<Bitmap> onCreateLoader(int i, Bundle bundle) { 
     return new UserFaceLoader(NewQingUActivity.this, mFaceUrl); 
    } 

    @Override 
    public void onLoadFinished(Loader<Bitmap> loader, Bitmap bitmap) { 
     if (bitmap != null) { 
      ImageView ivUserFace = (ImageView) findViewById(R.id.ivDrawerUserFace); 
      ivUserFace.setImageBitmap(bitmap); 
     } 
    } 

    @Override 
    public void onLoaderReset(Loader<Bitmap> loader) { 

    } 
}; 
+0

你解决这个问题? :D – Vucko

+0

@Vucko我没有:) – ProtossShuttle

+0

只是为了澄清,你是否在尝试获取视图之前设置了内容视图(即'setContentView()')? – burntblark

回答

1

我有一个类似的问题。相反的:

View viewById = findViewById(R.id.ivDrawerUserFace);

也许尝试:

View parentView = findViewById(R.id.ID_OF_PARENT_LINEAR_LAYOUT); 
View viewById = parentView.findViewById(R.id.ivDrawerUserFace); 

只需添加ID为包含此视图的线性布局和替换

ID_OF_PARENT_LINEAR_LAYOUT

与它。

1

当您使用更高的版本,例如编译“com.android.support:design:25.3.1”导航视图你有如下来实现您的看法:

View parentView = your_navigationView.getHeaderView(0); 
TextView text = (TextView)parentView.findViewById(R.id.textView);