首页 > 解决方案 > 无法从膨胀的布局中找到 ImageView

问题描述

我已经创建了一个布局,并且正在尝试绑定到它的 ID。

我已经设法绑定到TextView's 但由于某种原因我无法找到 imageView id。

 public FeedsAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
        this.myFeeds = objects;
        this.activity = context;
        this.layoutId = resource;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(this.activity);
        convertView = inflater.inflate(this.layoutId, parent, false);
        Feed currentFeed = this.myFeeds.get(position);

        TextView authorTextView = convertView.findViewById(R.id.author);
        TextView dateTextView = convertView.findViewById(R.id.date);
        TextView contentTextView = convertView.findViewById(R.id.content);

        ImageView imgImageView = (ImageView) convertView.findViewById(R.id.img);

        authorTextView.setText(currentFeed.getAuthor());
        dateTextView.setText(currentFeed.getDate());
        contentTextView.setText(currentFeed.getContent());
        return convertView;
    }

ImageView imgImageView = (ImageView) convertView.findViewById(R.id.img);

这一行给我一个错误,他不知道是什么R.id.img

这是布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.014"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.033" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.045"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.138" />

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.099"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.845" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="118dp"
        android:layout_height="106dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.091"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/a150" />

</androidx.constraintlayout.widget.ConstraintLayout>

顺便说一句,我正在寻找一个选项来为 imageView 放置一个占位符而不放任何src可能吗?

谢谢!

标签: javaandroidlayout-inflaterandroid-inflate

解决方案


我在 android studio 3.6 上有这个错误,但是你可以编译你的代码而不会失败!关闭android studio并重新打开它,您的错误必须修复,对吧?!


推荐阅读