java - 无法从膨胀的布局中找到 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
可能吗?
谢谢!
解决方案
我在 android studio 3.6 上有这个错误,但是你可以编译你的代码而不会失败!关闭android studio并重新打开它,您的错误必须修复,对吧?!
推荐阅读
- python - 同时旋转和分组列
- python - 从 pandas 数据帧构建频率字典而不循环
- php - PHP如何将另一个文件中的对象添加到不同JSON的数组中
- java - Ibinder 和 JobintentService
- spring-data-jpa - 多列上的 Spring Data JPA“或”运算符
- c - 在每个组件之后打印格式
- python - 如何在 Python3 中删除 HTML 标签
- html - 样式属性嵌套在一个 id 一个类和另一个属性中
- qt - Qt QML 如何回滚到 ListView 标题?
- r - 如何使用 R 和 Rpostgres 使用参数化查询,以便接受变量中包含的值作为 psql 的输入