Home > OS >  androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.LinearLayout
androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.LinearLayout

Time:03-23

When I click Update Faculty (MaterialCardView) in the activity_main.xml the activity activity_update_faculty.xml suppose to display but my app crashes and closes, I tried to find the problem in the logcat and it says "androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.LinearLayout" I'm struggling to find the problem since I'm still new. Can someone help me, please

Here's is the activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@ id/addNotice"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">

            <LinearLayout
                android:layout_width="135dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_green"
                    android:padding="15dp"
                    android:src="@drawable/ic_notice" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Notice"
                    android:textColor="@color/textColor"
                    android:textStyle="bold" />


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/addGalleryImage"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_purple"
                    android:padding="15dp"
                    android:src="@drawable/ic_gallery"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Image"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>



    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@ id/addEbook"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_pink"
                    android:padding="15dp"
                    android:src="@drawable/ic_upload"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Ebook"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/faculty"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_yellow"
                    android:padding="15dp"
                    android:src="@drawable/ic_group"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Update Faculty"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>



    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@ id/deleteNotice"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/cicle_red"
                    android:padding="15dp"
                    android:src="@drawable/ic_delete"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Delete Notice"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>





    </LinearLayout>



</LinearLayout>

Here is the MainActivity.java :

package com.example.adminuniversityapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.adminuniversityapp.facculty.UpdateFaculty;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    CardView uploadNptice, addGalleryImage, addEbook, faculty;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        uploadNptice = findViewById(R.id.addNotice);
        addGalleryImage = findViewById(R.id.addGalleryImage);
        addEbook = findViewById(R.id.addEbook);
        faculty = findViewById(R.id.faculty);

        uploadNptice.setOnClickListener(this);
        addGalleryImage.setOnClickListener(this);
        addEbook.setOnClickListener(this);
        faculty.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.addNotice:
                 intent = new Intent( MainActivity.this,UploadNotice.class);
                 startActivity(intent);
                 break;

            case R.id.addGalleryImage:
                 intent = new Intent( MainActivity.this,UploadImage.class);
                 startActivity(intent);
                 break;
            case R.id.addEbook:
                intent = new Intent( MainActivity.this,UploadPdfActivity.class);
                startActivity(intent);
                break;
            case R.id.faculty:
                intent = new Intent( MainActivity.this, UpdateFaculty.class);
                startActivity(intent);
                break;
        }


    }
}

activity_update_faculty.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".facculty.UpdateFaculty">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@ id/fab"
        style="@style/Widget.Design.FloatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add"
        app:elevation="3dp"
        app:fabSize="normal"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Computer Science"
                android:textColor="@color/textColor"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginVertical="8dp"
                android:background="#c1c1c1"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/csDepartment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"/>

            <LinearLayout
                android:id="@ id/csNoData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/no_data_found"/>

            </LinearLayout>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Mechanical Engineering"
                android:textColor="@color/textColor"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginVertical="8dp"
                android:background="#c1c1c1"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/mechanicalDepartment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"/>

            <LinearLayout
                android:id="@ id/mechNoData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/no_data_found"/>

            </LinearLayout>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Physics Department"
                android:textColor="@color/textColor"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginVertical="8dp"
                android:background="#c1c1c1"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/physicsDepatment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"/>

            <LinearLayout
                android:id="@ id/physicsNoData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/no_data_found"/>

            </LinearLayout>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Chemistry Department"
                android:textColor="@color/textColor"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginVertical="8dp"
                android:background="#c1c1c1"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/chemistryDepatment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"/>

            <LinearLayout
                android:id="@ id/chemistryNoData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/no_data_found"/>

            </LinearLayout>


        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

**UpdateFaculty.java: **

package com.example.adminuniversityapp.facculty;

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.adminuniversityapp.R; // i had to import this from the other package in order for  R.layout works
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

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

public class UpdateFaculty extends AppCompatActivity {

    FloatingActionButton fab;
    private RecyclerView csDepartment, mechanicalDepartment, physicalDepartment, chemistryDepartment;
    private LinearLayout csNoData, mechNoData, physicsNoData, chemistryNoData;
    private List<TeacherData> list, list2, list3, list4;
    private TeacherAdapter adapter;

    private DatabaseReference reference, dbRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_faculty);

        csDepartment = findViewById(R.id.csDepartment);
        mechanicalDepartment = findViewById(R.id.mechanicalDepartment);
        physicalDepartment = findViewById(R.id.physicsDepatment);
        chemistryDepartment = findViewById(R.id.chemistryDepatment);

        csNoData = findViewById(R.id.csDepartment);
        mechNoData = findViewById(R.id.mechNoData);
        physicsNoData = findViewById(R.id.physicsNoData);
        chemistryNoData = findViewById(R.id.chemistryNoData);

        reference = FirebaseDatabase.getInstance().getReference().child("teacher");


        csDepartment();
        mechanicalDepartment();
        physicalDepartment();
        chemistryDepartment();

        fab = findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(UpdateFaculty.this, addTeacher.class));
            }
        });
    }

    private void csDepartment() {
        dbRef = reference.child("Computer Science");
        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list = new ArrayList<>();
                if(!dataSnapshot.exists()){
                    csNoData.setVisibility(View.VISIBLE);
                    csDepartment.setVisibility(View.GONE);
                } else {
                    csNoData.setVisibility(View.GONE);
                    csDepartment.setVisibility(View.VISIBLE);
                    for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                        TeacherData data = snapshot.getValue(TeacherData.class);
                        list.add(data);
                    }
                    csDepartment.setHasFixedSize(true);
                    csDepartment.setLayoutManager(new LinearLayoutManager(UpdateFaculty.this));
                    adapter = new TeacherAdapter(list, UpdateFaculty.this);
                    csDepartment.setAdapter(adapter);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                Toast.makeText(UpdateFaculty.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void mechanicalDepartment() {
        dbRef = reference.child("Mechanical");
        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list2 = new ArrayList<>();
                if(!dataSnapshot.exists()){
                    mechNoData.setVisibility(View.VISIBLE);
                    mechanicalDepartment.setVisibility(View.GONE);
                } else {
                    mechNoData.setVisibility(View.GONE);
                    mechanicalDepartment.setVisibility(View.VISIBLE);
                    for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                        TeacherData data = snapshot.getValue(TeacherData.class);
                        list2.add(data);
                    }
                    mechanicalDepartment.setHasFixedSize(true);
                    mechanicalDepartment.setLayoutManager(new LinearLayoutManager(UpdateFaculty.this));
                    adapter = new TeacherAdapter(list2, UpdateFaculty.this);
                    mechanicalDepartment.setAdapter(adapter);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                Toast.makeText(UpdateFaculty.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void physicalDepartment() {
        dbRef = reference.child("Physics");
        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list3 = new ArrayList<>();
                if(!dataSnapshot.exists()){
                    physicsNoData.setVisibility(View.VISIBLE);
                    physicalDepartment.setVisibility(View.GONE);
                } else {
                    physicsNoData.setVisibility(View.GONE);
                    physicalDepartment.setVisibility(View.VISIBLE);
                    for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                        TeacherData data = snapshot.getValue(TeacherData.class);
                        list3.add(data);
                    }
                    physicalDepartment.setHasFixedSize(true);
                    physicalDepartment.setLayoutManager(new LinearLayoutManager(UpdateFaculty.this));
                    adapter = new TeacherAdapter(list3, UpdateFaculty.this);
                    physicalDepartment.setAdapter(adapter);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                Toast.makeText(UpdateFaculty.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void chemistryDepartment() {
        dbRef = reference.child("Chemistry");
        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list4 = new ArrayList<>();
                if(!dataSnapshot.exists()){
                    chemistryNoData.setVisibility(View.VISIBLE);
                    chemistryDepartment.setVisibility(View.GONE);
                } else {
                    chemistryNoData.setVisibility(View.GONE);
                    chemistryDepartment.setVisibility(View.VISIBLE);
                    for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                        TeacherData data = snapshot.getValue(TeacherData.class);
                        list4.add(data);
                    }
                    chemistryDepartment.setHasFixedSize(true);
                    chemistryDepartment.setLayoutManager(new LinearLayoutManager(UpdateFaculty.this));
                    adapter = new TeacherAdapter(list4, UpdateFaculty.this);
                    chemistryDepartment.setAdapter(adapter);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                Toast.makeText(UpdateFaculty.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}

Here is what it shows on the logcat right after I click the Update Faculty MaterialCardView :

2022-03-22 14:09:03.319 24645-24645/com.example.adminuniversityapp I/Timeline: Timeline: Activity_launch_request time:36107995
2022-03-22 14:09:03.344 24645-24702/com.example.adminuniversityapp V/FA: Recording user engagement, ms: 49869
2022-03-22 14:09:03.367 24645-24702/com.example.adminuniversityapp V/FA: Connecting to remote service
2022-03-22 14:09:03.369 24645-24671/com.example.adminuniversityapp V/FA: onActivityCreated
2022-03-22 14:09:03.373 24645-24702/com.example.adminuniversityapp V/FA: Connection attempt already in progress
2022-03-22 14:09:03.373 24645-24702/com.example.adminuniversityapp V/FA: Activity paused, time: 61111997
2022-03-22 14:09:03.375 24645-24645/com.example.adminuniversityapp D/DecorView[]: getWindowModeFromSystem  windowmode is 1
2022-03-22 14:09:03.409 24645-24645/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-03-22 14:09:03.409 24645-24645/com.example.adminuniversityapp I/FloatingActionButton: Setting a custom background is not supported.
2022-03-22 14:09:03.411 24645-24645/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-03-22 14:09:03.468 24645-24645/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-03-22 14:09:03.509 24645-24645/com.example.adminuniversityapp D/AndroidRuntime: Shutting down VM
2022-03-22 14:09:03.513 24645-24645/com.example.adminuniversityapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.adminuniversityapp, PID: 24645
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adminuniversityapp/com.example.adminuniversityapp.facculty.UpdateFaculty}: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.LinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3580)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3752)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2191)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8049)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
     Caused by: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.LinearLayout
        at com.example.adminuniversityapp.facculty.UpdateFaculty.onCreate(UpdateFaculty.java:46)
      
    

CodePudding user response:

csNoData = findViewById(R.id.csDepartment);

This is where the exception occurs. The reason is that the view with id csDepartment is a RecyclerView not a LinearLayout. Because of that, itcannot be cast to aLinearLayout`.

To solve it, replace that line of code with this:

csNoData = findViewById(R.id.csNoData);
  • Related