AndroidStudio: ‘onCreateViewHolder’方法无法在其类内部识别我的’.xml’布局文件。

huangapple go评论73阅读模式
英文:

AndroidStudio: 'onCreateViewHolder' method won't recognise my '.xml' layout file from within its class

问题

以下是您提供的内容的中文翻译部分:

我正在YouTube上关注一个AndroidStudio应用程序创建系列教程,链接在这里:https://www.youtube.com/watch?v=reSPN7mgshI

我已经创建了一个名为'note_item.xml'的布局文件,其中使用CardView来设置要放入我的'activity_main.xml'文件中的RecyclerView中的卡片布局。

我的'NoteAdapter.java'类从Note对象中获取数据,并使用这些数据创建要放入RecyclerView中的CardView。

我遇到的问题是,我的一个方法无法识别我的'note_item.xml'文件。我收到一个错误,上面写着:“无法解析符号'note_item',创建布局资源文件'note_item.xml'。”正如您在底部的图像中所看到的,我已经有一个note_item.xml文件。

可能是因为note_item.xml在创建时的根元素设置为'androidx.cardview.widget.CardView'吗?据我所见,视频中的人仍然没有使用AndroidX。

以下是您提供的代码:

package com.example.libraryroomapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/* NoteAdapter从Note对象中获取数据到RecyclerView项目中
* */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
    private List<Note> notes = new ArrayList<>();

    @NonNull
    @Override
    //创建并返回NoteHolder的方法
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.note_item, parent, false);
        return new NoteHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteHolder holder, int position) {
        Note currentNote = notes.get(position);
        holder.textViewTitle.setText(currentNote.getTitle());
        holder.textViewDescription.setText(currentNote.getDescription());
        holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    public void setNotes(List<Note> notes) {
        this.notes = notes;
        notifyDataSetChanged();
    }

    //ViewHolder类保存我们单个RecyclerView项目中的视图
    class NoteHolder extends RecyclerView.ViewHolder {
        private TextView textViewTitle;
        private TextView textViewDescription;
        private TextView textViewPriority;

        //构造函数
        public NoteHolder(View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.text_view_title);
            textViewDescription = itemView.findViewById(R.id.text_view_description);
            textViewPriority = itemView.findViewById(R.id.text_view_priority);
        }
    }
}

以下是我的note_item.xml的代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/text_view_priority"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:text="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_toStartOf="@id/text_view_priority"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Title"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/text_view_title"
            android:text="Description" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

AndroidStudio: ‘onCreateViewHolder’方法无法在其类内部识别我的’.xml’布局文件。

英文:

I'm following an AndroidStudio App creation series on YouTube here: https://www.youtube.com/watch?v=reSPN7mgshI

I've created a layout file 'note_item.xml' that uses CardView to set up card layouts that will be put into a RecyclerView in my 'activity_main.xml' file.

My 'NoteAdapter.java' class gets the data from my Note objects out of a List and uses the data to create cardViews to be put into the RecyclerView.

The problem I'm having is that one of my methods won't recognise my 'note_item.xml' file. I get an error that says "Cannot resolve symbol 'note_item' Create layout resource file 'note_item.xml'".
I already have a note_item.xml file, as you can see in the image at the bottom.

Could it be that the note_item.xml root element upon creation is set to 'androidx.cardview.widget.CardView'? The person in the video is still not using AndroidX as far as I can see.

Here's the code...

package com.example.libraryroomapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
/* NoteAdapter gets data from Note objects into the RecyclerView items
* */
public class NoteAdapter extends RecyclerView.Adapter&lt;NoteAdapter.NoteHolder&gt; {
private List&lt;Note&gt; notes = new ArrayList&lt;&gt;();
@NonNull
@Override
//method to create and return a NoteHolder
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_item, parent, false);
return new NoteHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull NoteHolder holder, int position) {
Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
}
@Override
public int getItemCount() {
return notes.size();
}
public void setNotes(List&lt;Note&gt; notes) {
this.notes = notes;
notifyDataSetChanged();
}
//ViewHolder class holds the views in our single recyclerView items
class NoteHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private TextView textViewPriority;
//constructor
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
}
}
}

The code for my note_item.xml is here too.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;8dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginEnd=&quot;8dp&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;8dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/text_view_priority&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:text=&quot;1&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Large&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/text_view_title&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_toStartOf=&quot;@id/text_view_priority&quot;
android:ellipsize=&quot;end&quot;
android:maxLines=&quot;1&quot;
android:text=&quot;Title&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Large&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/text_view_description&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@id/text_view_title&quot;
android:text=&quot;Description&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

AndroidStudio: ‘onCreateViewHolder’方法无法在其类内部识别我的’.xml’布局文件。

答案1

得分: 2

在Android Studio中会周期性地出现这样的问题。只需点击重新构建项目或退出并重新进入Android Studio。然后尝试生成APK文件。如果构建成功,则没有问题。如果出现错误,APK将不会被生成。

英文:

Such a problem periodically occurs in Android Studio. Just click rebuild the project or exit and re-enter the android studio. And try to generate apk. If it builds successfully, then there are no problems. And if there was an error, the apk would simply not be generated.

答案2

得分: 0

解决方法是......

重新启动了我的计算机...

我不知道为什么这样可以解决,但当我打开AndroidStudio时,错误不再存在。也许是gradle构建文件重新同步之类的原因。你的猜测和我一样好。

英文:

The fix was...

restarting my computer...

I have no idea why this worked, but when I opened AndroidStudio up the error was no longer there. Perhaps it was the gradle build file re-syncing or something. Your guess is as good as mine.

答案3

得分: 0

我认为这是最近版本的Android Studio中的一个错误,我每天遇到很多次。解决办法是重新启动Android Studio,我希望Google能尽快修复这个错误。

英文:

i think it's a bug for Android Stduio in recent versions,I meet many times a day. The solution is to restart the Android Stduio,I hope Google can fix this bug as soon as possible。

答案4

得分: 0

对于那些遇到这个问题而且这里的任何答案都不起作用的人,也许您的包含文件中有一个"android.R"(或类似的内容)。这对我造成了问题,将其移除后问题得到了解决。希望能对某人有所帮助。

英文:

For those of you who are facing this issue and none of the answers here worked, maybe you have a "android.R" in your includes (or something similar). That was causing the issue for me and removing it solved the issue. Hope it helps someone

huangapple
  • 本文由 发表于 2020年4月5日 19:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定