无法从膨胀的布局中找到ImageView

问题描述 投票:0回答:2

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

我设法绑定到TextView,但是由于某种原因我找不到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>

btw,我正在寻找一个为imageView放置一个占位符而不放置任何src的选项吗?

谢谢!

java android layout-inflater android-inflate
2个回答
1
投票

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


0
投票

有时,当编辑器中的缓存与资源文件不同步时,可能会发生这种情况。解决此问题的最佳方法是转到“文件”>“使缓存无效/重新启动”>

© www.soinside.com 2019 - 2024. All rights reserved.