为什么在使用Android中的RecyclerView时我会收到运行时异常?

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

why i got runtime exception while using Recycler View in android

问题

我正在学习RecyclerView,并且刚刚构建了一个基本列表,用于显示姓名和号码,但是遇到了运行时异常。

以下是代码部分:

MainActivity.java

package com.example.listviewandrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity {
    private List<Person> personData;
    private RecyclerView recyclerView;
    private RecyclerViewAdapter recyclerViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);

        personData = new ArrayList();
        personData.add(new Person("Hemant", "1"));
        // 添加更多的Person数据...

        recyclerViewAdapter = new RecyclerViewAdapter(personData, this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(recyclerViewAdapter);
    }
}

RecyclerViewAdapter.java

package com.example.listviewandrecyclerview;

import android.content.Context;
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.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {

    private List<Person> personList;
    private Context context;

    public RecyclerViewAdapter(List<Person> list, Context context) {
        this.personList = list;
        this.context = context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_layout, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {

        Person currPerson = personList.get(position);

        holder.textViewName.setText(currPerson.getName());
        holder.textViewNumber.setText(currPerson.getNumber());

    }

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

    class Holder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public TextView textViewNumber;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.name);
            textViewNumber = itemView.findViewById(R.id.number);
        }
    }
}

Person.java

package com.example.listviewandrecyclerview;

public class Person {
    public String Name, Number;

    public Person(String name, String number) {
        this.Name = name;
        this.Number = number;
    }

    public String getName() {
        return Name;
    }

    public String getNumber() {
        return Number;
    }
}

activity_main.xmlrecycler_layout.xml 这两个布局文件的内容没有被提供,不过代码中引用了它们。

如果你遇到了异常,请检查你的依赖项和布局文件,尤其是确保你的项目中有引入了 androidx.cardview.widget.CardView 类。

英文:

I am learning Recycler view and just build the basic list to display name and number but got the runtime exception

Here is the code

MainActivity.java

package com.example.listviewandrecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List&lt; Person&gt; personData;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
personData = new ArrayList();
personData.add( new Person(&quot;Hemant&quot;,&quot;1&quot;));
personData.add( new Person(&quot;Prashant&quot;,&quot;144&quot;));
personData.add( new Person(&quot;sagar&quot;,&quot;331&quot;));
personData.add( new Person(&quot;mayank&quot;,&quot;31&quot;));
personData.add( new Person(&quot;mayur&quot;,&quot;13&quot;));
personData.add( new Person(&quot;Hemant&quot;,&quot;1&quot;));
personData.add( new Person(&quot;Prashant&quot;,&quot;144&quot;));
personData.add( new Person(&quot;sagar&quot;,&quot;331&quot;));
personData.add( new Person(&quot;mayank&quot;,&quot;31&quot;));
personData.add( new Person(&quot;mayur&quot;,&quot;13&quot;));
recyclerViewAdapter = new RecyclerViewAdapter(personData, this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
}
}

RecyclerViewAdpater.java

package com.example.listviewandrecyclerview;
import android.content.Context;
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.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.Holder&gt; {
private List&lt;Person&gt; personList;
private Context context;
public RecyclerViewAdapter(List&lt;Person&gt; list, Context context) {
this.personList = list;
this.context = context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_layout, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
Person currPerson = personList.get(position);
holder.textViewName.setText(currPerson.getName());
holder.textViewNumber.setText(currPerson.getNumber());
}
@Override
public int getItemCount() {
return personList.size();
}
class Holder extends RecyclerView.ViewHolder {
public TextView textViewName;
public TextView textViewNumber;
public Holder(@NonNull View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.name);
textViewNumber = (TextView) itemView.findViewById(R.id.number);
}
}
}

Person.java

package com.example.listviewandrecyclerview;
public class Person  {
public String Name, Number;
public Person(String name, String number) {
this.Name = name;
this.Number = number;
}
public String getName() {
return Name;
}
public String getNumber() {
return Number;
}
}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recyclerView&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;729dp&quot;
android:layout_marginStart=&quot;1dp&quot;
android:layout_marginLeft=&quot;1dp&quot;
android:layout_marginEnd=&quot;1dp&quot;
android:layout_marginRight=&quot;1dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

recycler_layout.xml

&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;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;5dp&quot; &gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;TextView
android:id=&quot;@+id/name&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
android:textSize=&quot;36sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/number&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
android:textSize=&quot;36sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/name&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

But i got excpeton like this
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.listviewandrecyclerview, PID: 8041
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.cardview.widget.CardView" on path: DexPathList[[zip file "/data/app/com.example.listviewandrecyclerview-r3wz8HXjSJP_-5GXxapOHA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.listviewandrecyclerview-r3wz8HXjSJP_-5GXxapOHA==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.listviewandrecyclerview.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:28)
at com.example.listviewandrecyclerview.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:14)

答案1

得分: 1

我已经尝试了你的代码,并且我得到了如截图中所示的解决方案。

请尝试以下解决方案:

  • 请尝试执行 Invalidate cache and restart(清除缓存并重启)

  • 另外,检查是否已经添加了 appcompat、constraint layout 以及 material 依赖

implementation 'androidx.appcompat:appcompat:1.2.0'

implementation 'androidx.constraintlayout:constraintlayout:2.0.1'

implementation 'com.google.android.material:material:1.2.1'

  • 确保你已经 更新了 SDK

为什么在使用Android中的RecyclerView时我会收到运行时异常?

英文:

I've tried your code and I got the solution as mentioned in the screenshot

Go with the following solutions:

  • Please try to do Invalidate cache and restart

  • Also, check you've added appcompat, constraint layout, and material dependencies

> implementation 'androidx.appcompat:appcompat:1.2.0'
>
> implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
>
> implementation 'com.google.android.material:material:1.2.1'

  • Make sure you have updated SDK

为什么在使用Android中的RecyclerView时我会收到运行时异常?

huangapple
  • 本文由 发表于 2020年9月29日 17:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64116453.html
匿名

发表评论

匿名网友

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

确定