Home > Software design >  Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast
Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast

Time:10-18

I run my app and it crashes when I wish to open SheetActivity.java The error shows the error in my sheet activity but my xml file for that activity does not contain app bar layout . My toolbar.xml contains app bar layout and I have included the toolbar in my activity_sheet.xml. My SheetActivity.java:-

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.Calendar;

public class SheetActivity extends AppCompatActivity {
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheet);

        showTable();
        setToolbar();
    }
    private void setToolbar() {
        toolbar = findViewById(R.id.toolSheet);
        TextView title = toolbar.findViewById(R.id.title_toolbar);
        TextView subtitle = toolbar.findViewById(R.id.subtitle_toolbar);
        ImageButton back = toolbar.findViewById(R.id.back);
        ImageButton save = toolbar.findViewById(R.id.save);

        title.setText("Attendance Sheet");
        subtitle.setVisibility(View.GONE);
        back.setVisibility(View.INVISIBLE);
        save.setVisibility(View.INVISIBLE);
    }


    private void showTable() {
        DbHelper dbHelper = new DbHelper(this);
        TableLayout tableLayout = findViewById(R.id.tableLayout);
        long[] idArray = getIntent().getLongArrayExtra("idArray");
        int[] rollArray = getIntent().getIntArrayExtra("rollArray");
        String[] nameArray = getIntent().getStringArrayExtra("nameArray");
        String month = getIntent().getStringExtra("month");

        int DAY_IN_MONTH = getDayInMonth(month);
        int rowSize = idArray.length   1;

        TableRow[] rows = new TableRow[rowSize];
        TextView[] roll_tvs = new TextView[rowSize];
        TextView[] name_tvs = new TextView[rowSize];
        TextView[][] status_tvs = new TextView[rowSize][DAY_IN_MONTH   1];

        for (int i = 0; i<rowSize; i  ){
            roll_tvs[i] = new TextView(this);
            name_tvs[i] = new TextView(this);

            for (int j = 1; j <= DAY_IN_MONTH; j  ){
                status_tvs[i][j] = new TextView(this);
            }

        }

        roll_tvs[0].setText("Roll");
        roll_tvs[0].setTypeface(roll_tvs[0].getTypeface(), Typeface.BOLD);
        name_tvs[0].setText("Name");
        name_tvs[0].setTypeface(name_tvs[0].getTypeface(), Typeface.BOLD);
        for (int i = 1; i <= DAY_IN_MONTH; i  ){
            status_tvs[0][i].setText(String.valueOf(i));
            status_tvs[0][i].setTypeface(status_tvs[0][i].getTypeface(), Typeface.BOLD);

        }

        for (int i = 1; i<rowSize; i  ){
            roll_tvs[i].setText(String.valueOf(rollArray[i-1]));
            name_tvs[i].setText(nameArray[i-1]);
            for (int j = 1; j <= DAY_IN_MONTH; j  ){
                String day = String.valueOf(j);
                if (day.length()==1) day = "0" day;

                String date = day "." month;
                String status = dbHelper.getStatus(idArray[i-1], date);
                status_tvs[i][j].setText(status);

            }
        }

        for (int i = 0; i<rowSize; i  ){
            rows[i] = new TableRow(this);

            if(i%2==0)
                rows[i].setBackgroundColor(Color.parseColor("#EEEEEE"));
            else rows[i].setBackgroundColor(Color.parseColor("#E4E4E4"));

            roll_tvs[i].setPadding(16,16,16,16);
            name_tvs[i].setPadding(16,16,16,16);


            rows[i].addView(roll_tvs[i]);
            rows[i].addView(name_tvs[i]);

            for (int j = 1; j <= DAY_IN_MONTH; j  ){
                status_tvs[i][j].setPadding(16,16,16,16);
                rows[i].addView(status_tvs[i][j]);
            }

            tableLayout.addView(rows[i]);
        }
        tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_MIDDLE);





    }

    private int getDayInMonth(String month) {
        int monthIndex = Integer.parseInt(month.substring(0, 2)) - 1;
        int year = Integer.parseInt(month.substring(3));

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, monthIndex);
        calendar.set(Calendar.YEAR, year);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
}

My xml file for SheetActivity.java - activity_sheet.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=".SheetActivity">

    <include
        android:id="@ id/toolSheet"
        layout="@layout/toolbar"
        android:layout_width="427dp"
        android:layout_height="wrap_content" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none">
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none">
            <LinearLayout
                android:layout_width="wrap_content"
                android:padding="16dp"
                android:layout_height="wrap_content">
                <TableLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@ id/tableLayout"
                    android:background="#FFFFFF"
                    android:divider="@android:color/black"
                    android:showDividers="beginning|middle|end"
                    android:stretchColumns="*"/>

            </LinearLayout>


        </HorizontalScrollView>
    </ScrollView>

</LinearLayout>

The error which shows up:-

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manage.attendx/com.manage.attendx.SheetActivity}: java.lang.ClassCastException: com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6864)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.ClassCastException: com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar
    at com.manage.attendx.SheetActivity.setToolbar(SheetActivity.java:28)
    at com.manage.attendx.SheetActivity.onCreate(SheetActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:7232)
    at android.app.Activity.performCreate(Activity.java:7221)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
    ... 11 more

My xml file for toolbar:-

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@ id/toolbar">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp">
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/back"
                android:background="@android:color/transparent"
                android:layout_centerVertical="true"
                android:src="@drawable/icon_back"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="36dp"
                android:layout_marginLeft="36dp"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@ id/title_toolbar"
                    android:gravity="center_horizontal"
                    android:text="Title"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:textColor="#FFFFFF"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@ id/subtitle_toolbar"
                    android:gravity="center_horizontal"
                    android:text="Subtitle"
                    android:textSize="12sp"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"/>

            </LinearLayout>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@ id/save"
                android:background="@android:color/transparent"
                android:layout_alignParentRight="true"
                android:src="@drawable/icon_save"
                android:layout_centerVertical="true"/>
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

Can somebody please help me out with this problem?

CodePudding user response:

Answer - Inorder to resolve the issue , we have to remove the method setToolbar (); from SheetActivity.java .

Otherwise, AppBarLayout cannot be casted to androidx.appcopat.widget.toolbar

  • Related