LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout $ LayoutParams

时间:2014-05-30 13:17:59

标签: android navigation-drawer

我试图做导航抽屉选项。

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
             android:id="@+id/sendnewmessages"
             android:layout_width="wrap_content"
             android:layout_height="30dp"
             android:background="@drawable/nothere"
             android:gravity="center_horizontal"
             android:shadowColor="@android:color/black"
             android:shadowDx="1"
             android:shadowDy="1"
             android:shadowRadius="1"
             android:text="Send New Messages"
             android:textAlignment="center"
             android:textColor="@android:color/black" />
        <EditText
             android:id="@+id/searchmessage"
             android:layout_height="40dp"
             android:layout_width="200dp"
             android:gravity="center"
             android:hint="Search Messages"/>
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

如果我删除Linearlayout则没有错误。此按钮显示在listview后面,edittext出现在屏幕中间。问题是对象没有对齐。有没有办法在DrawerLayout中对齐视图对象?

如果我不删除linearlayout,我会收到此错误:

LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

堆栈跟踪:

  
    

E / AndroidRuntime(3454):FATAL EXCEPTION:main进程:com.brilliant.bridge,PID:3454 java.lang.ClassCastException:     android.widget.LinearLayout $ LayoutParams无法强制转换为     android.support.v4.widget.DrawerLayout $ LayoutParams at     android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910)     在     android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1156)     在     com.brilliant.bridge.AdminMessages.onPrepareOptionsMenu(AdminMessages.java:220)     在android.app.Activity.onPreparePanel(Activity.java:2556)at     com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)     在     com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)     在     com.android.internal.policy.impl.PhoneWindow $ 1.run(PhoneWindow.java:221)     在     android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)     在android.view.Choreographer.doCallbacks(Choreographer.java:574)at     android.view.Choreographer.doFrame(Choreographer.java:543)at     android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)     在android.os.Handler.handleCallback(Handler.java:733)处     android.os.Handler.dispatchMessage(Handler.java:95)at     android.os.Looper.loop(Looper.java:136)at     android.app.ActivityThread.main(ActivityThread.java:5017)at     java.lang.reflect.Method.invokeNative(Native Method)at     java.lang.reflect.Method.invoke(Method.java:515)at     com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)at     dalvik.system.NativeStart.main(原生方法)

  
public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

1 个答案:

答案 0 :(得分:10)

在xml文件中为您的LinearLayout分配ID:

<LinearLayout
        android:id="@+id/DrawerLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- YOUR LISTVIEW IS HERE -->
</LinearLayout>

然后在你的java类中,抛出这个LinearLayout:

private LinearLayout    DrawerLinear;

在OnCreate中你应该这样做:

DrawerLinear = (LinearLayout) findViewById(R.id.DrawerLinear);

在你的onPrepareOptionMenu中,把它放在:

public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(DrawerLinear);
    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

它可以解决你的问题。