XML静态加载fragment 后,运行时闪退

happyliuliming 2017-10-05 11:19:03
Android新手,在学习Fragment, 参考如下文章的例子,运行时闪退。
http://www.cnblogs.com/guop/p/5072572.html

xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<!-- 静态加载Fragment -->
<fragment class="com.home.lemon.fragmenttest.LeftFragment"
android:id="@+id/left_fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment class="com.home.lemon.fragmenttest.RightFragment"
android:id="@+id/right_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>


Log 信息如下:

10-05 14:28:18.393 8438-8438/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.home.lemon.fragmenttest, PID: 8438
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.home.lemon.fragmenttest/com.home.lemon.fragmenttest.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment
...全文
342 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyliuliming 2017-10-06
  • 打赏
  • 举报
回复
自己找到原因了. 在用android studio创建一个blank Fragment类的时候,不要勾选 include fragment factory methods/include interface callbacks. 仅保留如下代码即可。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_layout,container,false);
        return view;
    }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧