Reputation: 21
I am making a project but when I compile my project and launch the app on Android Studio my app crashes.
In my Logcat error
Process: com.passionategeekz.learnC, PID: 5854
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passionategeekz.learnC/com.passionategeekz.learnC.card}: java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to androidx.recyclerview.widget.RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
at android.os.Handler.dispatchMessage(Handler.java:106)``
My card.java
package com.passionategeekz.learnC;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class card extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card);
ArrayList<model> list= new ArrayList();
list.add(new model(model.TEXT_TYPE,"xxx",0));
list.add(new model(model.IMAGE_TYPE,"xx",R.drawable.wtc));
list.add(new model(model.AUDIO_TYPE,"xx",R.raw.sound));
list.add(new model(model.IMAGE_TYPE,"xx",R.drawable.snow));
MultiViewTypeAdapter adapter = new MultiViewTypeAdapter(list,this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,OrientationHelper.VERTICAL,false);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
}
}
My card.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"
android:fitsSystemWindows="true"
tools:context="com.passionategeekz.learnC.MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/recyclerView"
android:layout_width="350dp"
android:layout_height="447dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
My Gradle:
implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
Upvotes: 1
Views: 5280
Reputation: 2660
In your xml change
<androidx.cardview.widget.CardView
android:id="@+id/recyclerView"
android:layout_width="350dp"
android:layout_height="447dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
TO
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="350dp"
android:layout_height="447dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
Upvotes: 0
Reputation: 364
The problem is this row:
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
The view whith id "recyclerView" is a CardView which you are trying to cast to RecyclerView. Change it`s type to RecyclerView instead of CardView in your xml.
Upvotes: 1
Reputation: 239
Replace cardview with this
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="350dp"
android:layout_height="447dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
Upvotes: 0