java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout $ LayoutParams

时间:2015-05-13 18:51:22

标签: android android-layout navigation-drawer

我有一个应用程序,我收到此错误消息。当我跑,它运行正常,但当我选择一个项目o导航抽屉我得到例外。 这是我的错误日志:

 Process: com.example.henrique.testedrawer2, PID: 26739
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
            at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1100)
            at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1305)
            at com.example.henrique.testedrawerKiron.MainActivity.selectItem(MainActivity.java:133)
            at com.example.henrique.testedrawerKiron.MainActivity.access$100(MainActivity.java:26)
            at com.example.henrique.testedrawerKiron.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:96)
            at android.widget.AdapterView.performItemClick(AdapterView.java:305)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
            at android.widget.AbsListView$3.run(AbsListView.java:3860)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的activity_main布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar" />


    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Teste"/>

        <!-- navigation drawer -->
        <RelativeLayout
            android:layout_gravity="left|start"
            android:layout_width="match_parent"
            android:background="#fff"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/left_drawer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="#eee"
                android:background="#fff"
                android:dividerHeight="1dp" />
        </RelativeLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

这是我单击列表项时调用的activity_entidade布局(这实际上是一个片段):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.henrique.testedrawerKiron.EntidadeActivity">

   <ListView
       android:id="@+id/listaentidades"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我认为您在代码中关闭了错误的布局。

看看我的回答:ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout

只需使用DrawerLayout关闭自己或在closeDrawers()对象上使用DrawerLayout;