无法从膨胀的布局中找到 ImageView
unable to find ImageView from the inflated layout
我已经创建了一个布局,我正在尝试绑定到它的 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);
这一行return me an error that he doesn't know what is 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 工作室并重新打开它,您的错误必须得到修复,对吧?!
当编辑器中的缓存与资源文件不同步时,有时会发生这种情况。解决此问题的最佳方法是转到“文件”>“使 Caches/Restart
无效”
我已经创建了一个布局,我正在尝试绑定到它的 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);
这一行return me an error that he doesn't know what is 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 工作室并重新打开它,您的错误必须得到修复,对吧?!
当编辑器中的缓存与资源文件不同步时,有时会发生这种情况。解决此问题的最佳方法是转到“文件”>“使 Caches/Restart
无效”