导航抽屉中的类别转换异常

时间:2014-06-06 14:17:03

标签: android android-actionbar navigation-drawer classcastexception

我正在研究Android的导航抽屉。根据我的要求,我要在导航抽屉中显示EditText和项目列表视图。我在布局xml文件中创建了一个linearLayout,并将两个小部件(EditText和Listview)放在LinearLayout中。

当我运行代码时,我收到以下错误:

06-06 10:15:04.050: E/AndroidRuntime(15176): FATAL EXCEPTION: main
06-06 10:15:04.050: E/AndroidRuntime(15176): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1156)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.usaweb.leadna.MainActivity.onPrepareOptionsMenu(MainActivity.java:373)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.app.Activity.onPreparePanel(Activity.java:2522)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:491)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivity.superOnPrepareOptionsPanel(ActionBarActivity.java:240)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivityDelegate.onPrepareOptionsPanel(ActionBarActivityDelegate.java:157)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivity.onPrepareOptionsPanel(ActionBarActivity.java:212)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:480)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivityDelegateICS.onPreparePanel(ActionBarActivityDelegateICS.java:154)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivity.onPreparePanel(ActionBarActivity.java:204)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onPreparePanel(ActionBarActivityDelegateICS.java:298)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:441)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.view.Choreographer.doFrame(Choreographer.java:531)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.os.Handler.handleCallback(Handler.java:730)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.os.Looper.loop(Looper.java:137)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at android.app.ActivityThread.main(ActivityThread.java:5103)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at java.lang.reflect.Method.invoke(Method.java:525)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-06 10:15:04.050: E/AndroidRuntime(15176):    at dalvik.system.NativeStart.main(Native Method)

我的XML:

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

    <!-- Contenido Principal -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/leadna_home_bg"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="40dp"
                android:src="@drawable/mainbtns3" />

            <Button
                android:id="@+id/btnTasks"
                android:layout_width="110dp"
                android:layout_height="105dp"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_alignLeft="@+id/btnLeads"
                android:layout_marginBottom="26dp"
                android:background="@android:color/transparent" />

            <Button
                android:id="@+id/btnNearMe"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_above="@+id/btnTasks"
                android:layout_alignLeft="@+id/imageView1"
                android:background="@android:color/transparent" />

            <Button
                android:id="@+id/btnAddLeads"
                android:layout_width="105dp"
                android:layout_height="90dp"
                android:layout_alignBaseline="@+id/btnNearMe"
                android:layout_alignBottom="@+id/btnNearMe"
                android:layout_alignParentRight="true"
                android:background="@android:color/transparent" />

            <Button
                android:id="@+id/btnAppointments"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_alignTop="@+id/btnTasks"
                android:layout_toLeftOf="@+id/btnTasks"
                android:background="@android:color/transparent" />

            <Button
                android:id="@+id/btnLeads"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_above="@+id/btnAddLeads"
                android:layout_alignRight="@+id/imageView1"
                android:layout_marginRight="47dp"
                android:background="@android:color/transparent" />

            <Button
                android:id="@+id/btnSync"
                android:layout_width="100dp"
                android:layout_height="95dp"
                android:layout_alignBottom="@+id/btnLeads"
                android:layout_alignRight="@+id/btnAppointments"
                android:background="@android:color/transparent"
                android:onClick="showActionSheet" />
        </RelativeLayout>
    </FrameLayout>

    <!-- Menú Lateral -->

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/drawer_title_shape"
            android:gravity="center"
            android:text="Country"
            android:textSize="20sp" />

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/darker_gray"
            android:choiceMode="singleChoice" />
    </LinearLayout>

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

我的源代码:

opcionesMenu = new String[] {"Home", "Logout"};
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        
        drawerList = (ListView) findViewById(R.id.left_drawer);    

        mDrawer = ( LinearLayout) findViewById(R.id.drawer);

        drawerList.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_1, opcionesMenu));

        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                drawerLayout.closeDrawer(mDrawer);  
            }
        });

        tituloSeccion = getTitle();
        tituloApp = getTitle();

        drawerToggle = new ActionBarDrawerToggle(this, 
                drawerLayout,
                R.drawable.ic_navigation_drawer, 
                R.string.app_name,
                R.string.app_name) {

            public void onDrawerClosed(View view) {
                getActionBar().setTitle(tituloSeccion);
                supportInvalidateOptionsMenu();  
            }

            public void onDrawerOpened(View view) {
                getActionBar().setTitle(tituloApp);
                supportInvalidateOptionsMenu();  
            }
        };

        drawerLayout.setDrawerListener(drawerToggle);


        getActionBar().setCustomView(R.layout.custom_action_bar);
        getActionBar().setDisplayShowCustomEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);         
        getActionBar().setHomeButtonEnabled(true);

我在代码中做了什么错误。如果有人知道,请帮助我。

1 个答案:

答案 0 :(得分:1)

让布局代码与原始问题的方式相同。更改以下行373:

drawerLayout.isDrawerOpen(drawerList);

drawerLayout.isDrawerOpen(mDrawer);