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布局文件



    
    

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

你可能感兴趣的:(android,fragment运行时异常,静态引用fragemnt)