简体   繁体   中英

android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout

After merging two androidStudio Project i have this error, The app initialized with a splash and that shows and works well. Then when try to access the activity_main.xml the app crashes. the Error is:

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 (Layout) is:

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

line 96 of ActivityMain.java is (first line of below code):

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();

note: all of the code worked before merging two project..

You problem is

Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout

You root xml code is ConstraintLayout . So you should change to 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM