java - 当我尝试打开活动时应用程序崩溃
问题描述
当我在我的应用程序上按登录时,它应该带我去activity.targa
,但它却崩溃了,我不明白为什么(我的手机是 Redmi Note 7,但它也在 Mi A2 上崩溃)
活动.targa:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:fitsSystemWindows="true"
tools:context=".TargaActivity">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView.../>
<com.google.android.material.textfield.TextInputLayout.../>
<TextView.../>
<TextView.../>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
和 TargaActivity:
package com.efficientparking.efficientparking;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import java.util.HashMap;
public class TargaActivity extends AppCompatActivity {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_targa);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
@Override
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
我不得不在这里更改,因为 Android Studio 给我带来了麻烦,因为他说以下方法 ( setSupportActionBar(toolbar)
) 不存在。
但是现在只要我尝试进入activity_targa
,应用程序就会崩溃。
Android studio实际上加载了xml,看起来不错,但是我编译了apk,它崩溃了。
日志文件:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.efficientparking/com.efficientparking.efficientparking.TargaActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2047)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at com.efficientparking.efficientparking.TargaActivity.onCreate(TargaActivity.java:22)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
... 11 more
解决方案
您应该android.widget.Toolbar
在两者中使用xml
and java
,如setSupportActionBar
需要android.widget.Toolbar
推荐阅读
- swagger - 如何使用自定义分隔符序列化查询对象
- cordova - cordova gigya 和 cordova googleplus 插件中的 WEB_APPLICATION_CLIENT_ID 和 GOOGLE_IOS_CLIENT_ID 是什么
- mysql - 如何从 sql 中删除规范化?
- azure-ad-b2c - 添加具有自定义声明的用户
- android - ValueEventListner 在第一次加载活动时不会运行
- c++ - ADL 和非类型模板参数
- reactjs - useRef 在 React 的 ag-grid 中创建一个 cellEditor
- fullcalendar - slotDuration 15 分钟连续破格 30 分钟
- postgresql - Postgres 使用带有 xmlnamespaces 的 xpath_table 解析
- objective-c - 如何使用按钮取消“dispatch_get_main_queue”?