Ошибка — java.lang.ClassCastException:com.google.android.материал.панель приложений.AppBarLayout не может быть преобразован в androidx.appcompat.widget.Панель инструментов

#java #android #android-layout

Вопрос:

Я запускаю свое приложение, и оно вылетает, когда я хочу открыть SheetActivity.java Ошибка показывает ошибку в моем действии на листе, но мой xml-файл для этого действия не содержит макета панели приложений . Мой toolbar.xml содержит макет панели приложений, и я включил панель инструментов в свой activity_sheet.xml. Мой 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; ilt;rowSize; i  ){  roll_tvs[i] = new TextView(this);  name_tvs[i] = new TextView(this);   for (int j = 1; j lt;= 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 lt;= 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; ilt;rowSize; i  ){  roll_tvs[i].setText(String.valueOf(rollArray[i-1]));  name_tvs[i].setText(nameArray[i-1]);  for (int j = 1; j lt;= 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; ilt;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 lt;= 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);  } }  

Мой xml-файл для SheetActivity.java — activity_sheet.xml:-

 lt;?xml version="1.0" encoding="utf-8"?gt; lt;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"gt;   lt;include  android:id="@ id/toolSheet"  layout="@layout/toolbar"  android:layout_width="427dp"  android:layout_height="wrap_content" /gt;   lt;ScrollView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:scrollbars="none"gt;  lt;HorizontalScrollView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:scrollbars="none"gt;  lt;LinearLayout  android:layout_width="wrap_content"  android:padding="16dp"  android:layout_height="wrap_content"gt;  lt;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="*"/gt;   lt;/LinearLayoutgt;    lt;/HorizontalScrollViewgt;  lt;/ScrollViewgt;  lt;/LinearLayoutgt;  

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:-

 lt;?xml version="1.0" encoding="utf-8"?gt; lt;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"gt;  lt;androidx.appcompat.widget.Toolbar  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@ id/toolbar"gt;    lt;RelativeLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginRight="16dp"gt;  lt;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"/gt;  lt;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"gt;  lt;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"/gt;  lt;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"/gt;   lt;/LinearLayoutgt;   lt;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"/gt;  lt;/RelativeLayoutgt;  lt;/androidx.appcompat.widget.Toolbargt;  lt;/com.google.android.material.appbar.AppBarLayoutgt;  

Может кто-нибудь, пожалуйста, помочь мне с этой проблемой?

Комментарии:

1. Исключение составляет описание самого себя .. Вы использовали AppBarLayout в XML-макете, раздувая его Toolbar в поведении

Ответ №1:

Ответ — Для того , чтобы решить проблему, мы должны удалить метод setToolbar (); из SheetActivity.java .

В противном случае AppBarLayout не может быть применен к androidx.appcopat.widget.панель инструментов