Reputation: 51
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)
Upvotes: 0
Views: 174
Reputation: 161
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'
Upvotes: 1