[英]Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar
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 .我运行我的应用程序,当我希望打开 SheetActivity.java 时它崩溃了错误显示我的工作表活动中的错误,但我的该活动的 xml 文件不包含应用程序栏布局。 My toolbar.xml contains app bar layout and I have included the toolbar in my activity_sheet.xml.我的toolbar.xml 包含应用栏布局,并且我在activity_sheet.xml 中包含了工具栏。 My SheetActivity.java:-我的 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:-我的 SheetActivity.java xml 文件 - 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 文件:-
<?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?有人可以帮我解决这个问题吗?
Answer - Inorder to resolve the issue , we have to remove the method setToolbar ();答案 - 为了解决这个问题,我们必须删除方法 setToolbar(); from SheetActivity.java .来自 SheetActivity.java 。
Otherwise, AppBarLayout cannot be casted to androidx.appcopat.widget.toolbar否则,无法将 AppBarLayout 强制转换为 androidx.appcopat.widget.toolbar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.