英文:
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.xml 和 recycler_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< Person> 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("Hemant","1"));
personData.add( new Person("Prashant","144"));
personData.add( new Person("sagar","331"));
personData.add( new Person("mayank","31"));
personData.add( new Person("mayur","13"));
personData.add( new Person("Hemant","1"));
personData.add( new Person("Prashant","144"));
personData.add( new Person("sagar","331"));
personData.add( new Person("mayank","31"));
personData.add( new Person("mayur","13"));
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<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 = (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
<?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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论