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文件中引入Fragment路径(注意自己的包名要正确),所以加入一行如下代码问题即可解决:

    
        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属性,否则会闪退

你可能感兴趣的:(android)