对于抽屉标题中的视图返回null

对于抽屉标题中的视图返回null

本文介绍了为什么findViewById()对于抽屉标题中的视图返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DrawerLayout,其中将NavigationView包装为标头布局. XML如下:

I have a DrawerLayout which wraps a NavigationView with a header layout. The XML is below:

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

headerLayout是包裹ImageViewLinearLayout,下面是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>

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

I don't know why I can't find the ImageView by findViewById(R.id.ivDrawerUserFace), which returns null instead. However, I am able to find it by the following code:

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()找到它?

I have called setContentView() before the above code. And Glide loads the image into the ImageView successfully. Why can't I find it by findViewById()?

我想是因为图像视图不在传递给setContentView()的XML文件中.但是,我可以使用以下代码在同一Activity中通过findViewById()获取视图:

I guess it's because the image view isn't within the XML file passed to setContentView(). However, I could get the view by findViewById() in the same Activity with code like this:

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

    }
};

推荐答案

在更高版本中使用导航视图时,例如编译'com.android.support:design:25.3.1'来实现您的视图,如下所示:

when you are using navigation view in higher version like compile 'com.android.support:design:25.3.1' you have to implement your view as below:

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

这篇关于为什么findViewById()对于抽屉标题中的视图返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:19