android.support.constraint.ConstraintLayout 无法转换为 android.support.v4.widget.DrawerLayout
android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
合并两个 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();
注意:所有代码在合并两个项目之前都有效..
你的问题是
Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
您的 root 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>
合并两个 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();
注意:所有代码在合并两个项目之前都有效..
你的问题是
Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
您的 root 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>