合并两个androidStudio项目后我有这个错误, 该应用程序初始化为启动,显示并运行良好。然后,当尝试访问activity_main.xml时,应用程序崩溃。 错误是:
FATAL EXCEPTION: main
Process: eu.siacs.conversations, PID: 16604
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.siacs.conversations/eu.siacs.conversations.ui.MainActivity}: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
at eu.siacs.conversations.ui.MainActivity.onCreate(MainActivity.java:96)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
activity_main.xml(布局)是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context="eu.siacs.conversations.ui.MainActivity">
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<include
layout="@layout/app_bar_main"
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp" />
<!--android:layout_height="match_parent"-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_gravity="start"
android:background="@color/main_background_color"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/nav_font"
app:itemTextColor="@color/nav_font"
app:menu="@menu/activity_main_drawer" />
</android.support.constraint.ConstraintLayout>
ActivityMain.java的第96行是(下面代码的第一行):
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
注意:在合并两个项目之前,所有代码都有效。
答案 0 :(得分:4)
你的问题是
引起:java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法强制转换为android.support.v4.widget.DrawerLayout
你的根xml代码是ConstraintLayout
。所以你应该改为DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context="eu.siacs.conversations.ui.MainActivity">
...
</android.support.v4.widget.DrawerLayout>