简体   繁体   English

findViewById在AppCompatActivity中返回null

[英]findViewById returns null in AppCompatActivity

I tried to change the Bitmap of an ImageView, but the code keeps returning a null Object. 我试图更改ImageView的位图,但是代码始终返回空对象。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    // Drawer Options
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    // Drawer Header
    ImageView img = (ImageView) findViewById(R.id.header_imageView);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.marius_cropped_48x48);
    RoundedBitmap profile_pic = new RoundedBitmap(bm);
    if (img != null) {
        img.setImageBitmap(profile_pic.getRoundedBitmap());
        Log.d(TAG, "ImageView OK");
    } else {
        Log.d(TAG, "ImageView null");
    }

}

Here "img" is always null. 此处的“ img”始终为空。 I tried to change it to this but no luck. 我试图将其更改为此,但没有运气。

ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView);

or 要么

    LinearLayout header = (LinearLayout) drawer.findViewById(R.id.header_linearLayout);
    ImageView img = (ImageView) header.findViewById(R.id.header_imageView);

but no luck. 但没有运气。 Here are my xml files. 这是我的xml文件。

activity_drawer.xml activity_drawer.xml

<?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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_drawer"
        app:menu="@menu/activity_drawer_drawer" />

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

And the nav_header_drawer.xml file 还有nav_header_drawer.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/header_linearLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/header_imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing" />

    <TextView
        android:id="@+id/header_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="text"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/header_company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="as" />

</LinearLayout>

To successfuly look for widget in Navigation Drawer's header, you need to get the correct container view first by calling getHeaderView() on it: 为了在Navigation Drawer的标题中成功查找小部件,您需要首先通过对其调用getHeaderView()获得正确的容器视图:

View header = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0);

and then having header pointing to the right ViewGroup can call findViewById() on it for your target widget as usual: 然后让header指向正确的ViewGroup的header可以照常为目标窗口小部件调用findViewById()

ImageView iv = ((ImageView) header.findViewById(R.id.header_imageView));

Change following line from 更改以下行

ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView);

to

ImageView img = (ImageView) navigationView.findViewById(R.id.header_imageView);

As header_imageView is the part of navigation view not drawer. 因为header_imageView是导航视图而不是抽屉的一部分。

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

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