【问题标题】:AndroidStudio: 'onCreateViewHolder' method won't recognise my '.xml' layout file from within its classAndroidStudio:“onCreateViewHolder”方法无法从其类中识别我的“.xml”布局文件
【发布时间】:2020-07-17 09:03:24
【问题描述】:

我在这里关注 YouTube 上的 AndroidStudio 应用创建系列:https://www.youtube.com/watch?v=reSPN7mgshI

我创建了一个布局文件“note_item.xml”,它使用 CardView 设置卡片布局,该布局将被放入我的“activity_main.xml”文件中的 RecyclerView。

我的“NoteAdapter.java”类从我的 Note 对象中从 List 中获取数据,并使用该数据创建 cardViews 以放入 RecyclerView。

我遇到的问题是我的一种方法无法识别我的“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 gets data from Note objects into the RecyclerView items
* */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
    private List<Note> notes = new ArrayList<>();

    @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<Note> 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);
        }
    }
}

我的 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>

【问题讨论】:

  • 你尝试运行项目了吗?

标签: java android xml android-studio android-recyclerview


【解决方案1】:

Android Studio 中会定期出现此类问题。只需点击重建项目或退出并重新进入android studio。并尝试生成apk。如果它构建成功,那么就没有问题。如果出现错误,apk 将根本不会生成。

【讨论】:

  • 我在重新启动计算机时错误地发现了解决方案。给你一个很好的答案。谢谢:)
【解决方案2】:

修复是...

正在重新启动我的计算机...

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

【讨论】:

    【解决方案3】:

    我认为这是最近版本的 Android Stduio 的一个错误,我每天会遇到很多次。解决方法是重启Android Stduio,希望谷歌能尽快修复这个bug。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多