【问题标题】:Android ImageView not displayed and is nullAndroid ImageView 未显示且为空
【发布时间】:2018-04-20 07:03:39
【问题描述】:

ListView 项目中的 ImageView 没有显示,但是如果我只是将 ImageView 替换为 Button 或其他东西,它显示得很好。

列表项的布局文件

<?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 中看起来不错:

虽然在运行时不显示:

将“ImageView”替换为“Button”并运行:

使此布局膨胀的代码:

@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 替换时返回一个 valid Button 引用。

也就是说,this ListView 中只删除了 ImageViews
显示其他列表中的 ImageViews,即使它们是空的,也会显示边框;此 ListView 中显示的其他内容具有相同的属性集。

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

【问题讨论】:

    标签: listview android-studio layout-inflater


    【解决方案1】:

    我觉得问题出在这里

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

    不应该

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

    【讨论】:

    • 很遗憾,我对此一无所知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多