DrawerLayout实现侧滑菜单运行时闪退

今天使用DrawerLayout实现侧滑菜单时,出现了闪退问题,logcat报错如下

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.map_demo.lixio, PID: 4675
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.map_demo.lixio/com.map_demo.lixio.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2649)
                      at android.app.ActivityThread.access$900(ActivityThread.java:181)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1505)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:210)

部分原代码如下,从logcat提示可以看出xml中的fragment出了问题:

Binary XML file line #0: Error inflating class fragment
<?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"
    android:fitsSystemWindows="true">
    <!--主内容界面-->
<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </FrameLayout>
    <!--侧滑界面-->
        <fragment
            android:id="@+id/fg_right_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            app:layout="@layout/fg_right"
            android:clickable="true" /><!--防止点击穿透事件-->
</android.support.v4.widget.DrawerLayout>

仔细检查后发现要在.xml文件中引入Fragment路径(注意自己的包名要正确),所以加入一行如下代码问题即可解决:

    <!--侧滑界面-->
        <fragment
            android:id="@+id/fg_right_view"
            android:name="com.map_demo.lixio.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            app:layout="@layout/fg_right"
            android:clickable="true" /><!--防止点击穿透事件-->
原因是: 静态引用fragment类为Activity所对应的XML文件中添加fragment控件时,要添加name属性,否则会闪退

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值