本文介绍了导航抽屉XML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的stackoverflowers,

我用在我的活动抽屉式导航栏,这里的XML:

 < android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的FrameLayout
        机器人:ID =@ + ID / content_frame
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

    <的FrameLayout
        机器人:layout_width =240dp
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        机器人:后台=#3a3a3c>

        < ImageView的
            机器人:ID =@ + ID / ImageView01
            机器人:layout_width =240dp
            机器人:layout_height =10dp
            机器人:layout_marginTop =20dp
            机器人:SRC =@可绘制/边框/>

        <的TextView
            机器人:ID =@ + ID / tvretailers
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =0dp
            机器人:文本=零售商
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =20SP/>

        < ImageView的
            机器人:ID =@ + ID / imageView1
            机器人:layout_width =240dp
            机器人:layout_height =10dp
            机器人:layout_marginTop =300dp
            机器人:SRC =@可绘制/边框/>

        <的ListView
            机器人:ID =@ + ID / right_drawer_settings
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =250dp
            机器人:layout_marginTop =310dp
            机器人:后台=#3a3a3c>
        < / ListView控件>

        <的TextView
            机器人:ID =@ + ID / TextView01
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =280dp
            机器人:文本=设置
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =20SP/>

        <的ListView
            机器人:ID =@ + ID / right_drawer_retailers
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =200dp
            机器人:layout_marginTop =30dp
            机器人:后台=#3a3a3c>
        < / ListView控件>
    < /的FrameLayout>

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

然而,当我运行该应用程序我得到一个XML错误:

  1月八日至18号:36:47.751:E / AndroidRuntime(26527):致命异常:主要
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.androidarabia.phones / com.androidarabia.Lebanon.LebanonDrawer}:java.lang.ClassCastException:机器人.widget.FrameLayout $的LayoutParams不能转换为android.support.v4.widget.DrawerLayout $的LayoutParams
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread.access $ 700(ActivityThread.java:151)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1281)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.os.Handler.dispatchMessage(Handler.java:99)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.os.Looper.loop(Looper.java:137)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread.main(ActivityThread.java:5293)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在java.lang.reflect.Method.invokeNative(本机方法)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在java.lang.reflect.Method.invoke(Method.java:511)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在dalvik.system.NativeStart.main(本机方法)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):java.lang.ClassCastException:产生的原因android.widget.FrameLayout $的LayoutParams不能转换为android.support.v4.widget.DrawerLayout $的LayoutParams
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:844)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1048)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在com.androidarabia.Lebanon.LebanonDrawer.selectItem(LebanonDrawer.java:104)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在com.androidarabia.Lebanon.LebanonDrawer.onCreate(LebanonDrawer.java:63)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.Activity.performCreate(Activity.java:5250)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
1月8日至十八日:36:47.751:E / AndroidRuntime(26527):11 ...更多
 

在我曾在的FrameLayout唯一一个列表视图,和它的工作正常,但一些修改后,我没有能够得到它的工作!希望能帮到你!非常感谢stackoverflowers =)

全codeS:

 公共类LebanonDrawer扩展SherlockFragmentActivity {
    DrawerLayout mDrawerLayout;
    ListView控件mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    的String []称号;
    的String []字幕;
    INT []图标;
    片段黎巴嫩=新黎巴嫩();

    @燮pressWarnings(未登记)
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.drawer_main);

        mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList =(ListView控件)findViewById(R.id.right_drawer_retailers);
        的String [] son​​gsArray = {做,重,MI,FA,所以,LA,所以};
        MenuListAdapter MLA =新MenuListAdapter(这一点,songsArray);
        mDrawerList.setAdapter(MLA);

        mDrawerList.setOnItemClickListener(新DrawerItemClickListener());

        getSupportActionBar()setHomeButtonEnabled(真)。
        getSupportActionBar()setDisplayHomeAsUpEnabled(真)。

        mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
                R.drawable.ic_drawer,R.string.drawer_open,
                R.string.drawer_close){

            公共无效onDrawerClosed(查看视图){
                // TODO自动生成方法存根
                super.onDrawerClosed(视图);
            }

            公共无效onDrawerOpened(查看drawerView){
                // TODO自动生成方法存根
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        如果(savedInstanceState == NULL){
            选择信息(0);
        }
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){

        如果(item.getItemId()== android.R.id.home){

            如果(mDrawerLayout.isDrawerOpen(mDrawerList)){
                mDrawerLayout.closeDrawer(mDrawerList);
            } 其他 {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        返回super.onOptionsItemSelected(项目);
    }

    私有类DrawerItemClickListener工具
            ListView.OnItemClickListener {
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                长ID){
            选择信息(位置);
        }
    }

    私人无效选择信息(INT位置){

        FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
        //找到位置
        开关(位置){
        情况下0:
            ft.replace(R.id.content_frame,黎巴嫩);
            打破;

        }
        ft.commit();
        mDrawerList.setItemChecked(位置,真正的);
        //关闭抽屉
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @覆盖
    保护无效onPostCreate(包savedInstanceState){
        super.onPostCreate(savedInstanceState);
        //同步发生onRestoreInstanceState后切换状态。
        mDrawerToggle.syncState();
    }

    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);
        //传递任何配置更改抽屉切换
        mDrawerToggle.onConfigurationChanged(NEWCONFIG);
    }
}
 

解决方案

 产生的原因:java.lang.ClassCastException:android.widget.FrameLayout $的LayoutParams
                   不能转换为android.support.v4.widget.DrawerLayout $的LayoutParams
 

在你的XML布局, mDrawerList 不是 DrawerLayout 的直系后裔。相反,正在使用的FrameLayout 及其后代的的LayoutParams。

在当前设置,分配一个ID为保存的ListView的的FrameLayout,发现它在你的活动,并用它在:

  mDrawerLayout.openDrawer(frameHoldingListView);
 

进行类似更改为mDra​​werLayout.closeDrawer(...)和mDrawerLayout.isDrawerOpen(...)。

Dear stackoverflowers,

I'm using navigation drawer inside my activity, here's the 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" >

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

    <FrameLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#3a3a3c" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="240dp"
            android:layout_height="10dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/border" />

        <TextView
            android:id="@+id/tvretailers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:text="Retailers"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="240dp"
            android:layout_height="10dp"
            android:layout_marginTop="300dp"
            android:src="@drawable/border" />

        <ListView
            android:id="@+id/right_drawer_settings"
            android:layout_width="wrap_content"
            android:layout_height="250dp"
            android:layout_marginTop="310dp"
            android:background="#3a3a3c" >
        </ListView>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="280dp"
            android:text="Settings"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <ListView
            android:id="@+id/right_drawer_retailers"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_marginTop="30dp"
            android:background="#3a3a3c" >
        </ListView>
    </FrameLayout>

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

However, once i run the application i get an xml error:

08-18 01:36:47.751: E/AndroidRuntime(26527): FATAL EXCEPTION: main
08-18 01:36:47.751: E/AndroidRuntime(26527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidarabia.phones/com.androidarabia.Lebanon.LebanonDrawer}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.os.Looper.loop(Looper.java:137)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread.main(ActivityThread.java:5293)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at java.lang.reflect.Method.invoke(Method.java:511)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at dalvik.system.NativeStart.main(Native Method)
08-18 01:36:47.751: E/AndroidRuntime(26527): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:844)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1048)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at com.androidarabia.Lebanon.LebanonDrawer.selectItem(LebanonDrawer.java:104)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at com.androidarabia.Lebanon.LebanonDrawer.onCreate(LebanonDrawer.java:63)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.Activity.performCreate(Activity.java:5250)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-18 01:36:47.751: E/AndroidRuntime(26527):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
08-18 01:36:47.751: E/AndroidRuntime(26527):    ... 11 more

Before i had only one listview in the FrameLayout, and it worked properly, but after some modification i wasn't able to get it work! hope you can help! thanks alot stackoverflowers =)

Whole codes:

public class LebanonDrawer extends SherlockFragmentActivity {
    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    String[] title;
    String[] subtitle;
    int[] icon;
    Fragment Lebanon = new Lebanon();

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.right_drawer_retailers);
        String[] songsArray = { "Do", "RE", "MI", "FA", "SO", "LA", "SO" };
        MenuListAdapter mla = new MenuListAdapter(this, songsArray);
        mDrawerList.setAdapter(mla);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, Lebanon);
            break;

        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}
解决方案
Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
                   cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

In your xml layout, mDrawerList is not a direct descendant of DrawerLayout. Instead, its a descendant of FrameLayout whose LayoutParams are being used.

In your current setup, assign an id to the FrameLayout that holds the ListView, find it in your activity, and use it in:

mDrawerLayout.openDrawer(frameHoldingListView);

Make similar changes for mDrawerLayout.closeDrawer(...) and mDrawerLayout.isDrawerOpen(...).

这篇关于导航抽屉XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:00