Android ImageView未显示且为空

时间:2018-04-20 07:03:39

标签: listview android-studio layout-inflater

ListView项目中的ImageView没有显示,但是如果我只是用Button替换ImageView或其他东西它显示正常。

列表项的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="18dp"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <ImageView
        android:id="@+id/image"
        android:layout_width="48dp"
        android:layout_height="48dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Small" />

    </LinearLayout>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray" />

在Android Studio中看起来很好:
Android Studio screenshot

虽然它没有在运行时显示:
Runtime screenshot

用“按钮”替换“ImageView”并运行:
enter image description here

夸大此布局的代码:

@Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View root = getLayoutInflater().inflate(R.layout.entry_pickblock_list, viewGroup, false);
        Block blk = getItem(i);
        TextView tv = root.findViewById(R.id.text);
        tv.setText(blk.locale_name);
        tv = root.findViewById(R.id.text2);
        tv.setText(getString(R.string.pickblock_entry_detail, blk.id, blk.name));
        ImageView iv = findViewById(R.id.image);//iv is null.
        //iv.setImageDrawable(BlockIcons.get(blk.id));
        return root;
    }

是的,ImageView是空的,但它应该有我的调试工具显示的边框。更重要的是,findViewById为ImageView返回null,但当它被Button替换时返回有效 Button引用。

也就是说, ListView中仅删除 ImageViews
显示其他列表中的ImageView,并显示边框,即使它们是空的;其他内容在此ListView中显示,并设置了相同的属性。

我没有编写代码来删除它们,我根本无法这样做,因为我甚至无法从视图树中获取它。我没有安装Adblock或其他Xposed模块来操纵视图。我的系统或手机的供应商似乎没有这样做。那么是谁做到了?

1 个答案:

答案 0 :(得分:2)

我认为问题出在这里

iv = findViewById(R.id.image);//iv is null

不应该是

iv = root.findViewById(R.id.image);