我构建了导航抽屉,它也已成功安装在移动设备中,但是单击按钮后,应用程序将自动停止。但没有显示任何错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.adworld.admin.ab, PID: 25835
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adworld.admin.ab/com.adworld.admin.ab.Main2Activity}: java.lang.ClassCastException: com.google.android.material.appbar.CollapsingToolbarLayout cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3010)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1707)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6758)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
Caused by: java.lang.ClassCastException: com.google.android.material.appbar.CollapsingToolbarLayout cannot be cast to androidx.appcompat.widget.Toolbar
at com.adworld.admin.ab.Main2Activity.onCreate(Main2Activity.java:19)
at android.app.Activity.performCreate(Activity.java:7147)
at android.app.Activity.performCreate(Activity.java:7138)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2885)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3010)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1707)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6758)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
答案 0 :(得分:0)
Caused by: java.lang.ClassCastException:
com.google.android.material.appbar.CollapsingToolbarLayout cannot be cast
to androidx.appcompat.widget.Toolbar
检查您在定义工具栏的Java代码,并将其转换为com.google.android.material.appbar.CollapsingToolbarLayout
答案 1 :(得分:0)
CollapsingToolbarLayout
无法转换为androidx.appcompat.widget.Toolbar...
,这可以告诉您问题出在哪里。
找不到正确的类型,也无法将CollapsingToolbarLayout
强制转换为widget.Toolbar
如果不确定我指的是什么,请转到Main2Activity
的布局:
在布局中,切换到Text
模式;按下左下方的按钮。
请确保已定义<android.support.design.widget.AppBarLayout
,并且已将id
定义为Main2Activity.class
的{{1}}中使用的名称。从https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example
findViewById
使用上面的示例,我们关心这一行,以便对<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
:Main2Activity
然后在android:id="@+id/app_bar"
中看起来像这样:
onCreate()