Fragment运行时错误

这是在布局文件静态引入fragment出现的运行时异常(编译能通过),
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.demo, PID: 17573
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.fragment.StaticFragmentActivity}: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2836)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6665)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
                   Caused by: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
                   Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
                   Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Must specify unique android:id, android:tag, or have a parent with an id for com.example.demo.fragment.TitleFragment
                      at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3582)
                      at android.app.FragmentController.onCreateView(FragmentController.java:98)
                      at android.app.Activity.onCreateView(Activity.java:6255)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:784)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                      at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
                      at android.app.Activity.setContentView(Activity.java:2713)
                      at com.example.demo.fragment.StaticFragmentActivity.onCreate(StaticFragmentActivity.java:16)
                      at android.app.Activity.performCreate(Activity.java:7057)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2789)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6665)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Application terminated.

下面是我的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="vertical">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:name="com.example.demo.fragment.TitleFragment" />
    <fragment
        android:id="@+id/fgContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.demo.fragment.ContentFragment" />
</LinearLayout>

出错的原因是我没有给第一个fragent设置id(一开始我没有加id,是认为我在activity里不会调用它),给它设置一个id就可以了,例如设置为:android:id="@+id/fgTitle"

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012585988/article/details/79969258
个人分类: android
上一篇EditView 使其开始不获得焦点
下一篇Preview is unavailable until a successful build
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭