首页 > 代码库 > 学习Fragment遇到的一个报错

学习Fragment遇到的一个报错

从现在开始,在博客园上记录自己遇到的所有android开发问题:

布局文件内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:orientation="horizontal"
    android:baselineAligned="false" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment    android:name="com.Victory.myfragment.LeftFragment"  
        android:id="@+id/leftFragmentID"  
        android:layout_width="0dip"  
        android:layout_height="match_parent"  
        android:layout_weight="1" ></fragment>  
    <fragment   android:name="com.Victory.myfragment.RightFragment"
        android:id="@+id/rightFragmentID"  
        android:layout_width="0dip"  
        android:layout_height="match_parent"  
        android:layout_weight="1" ></fragment>  
    
</LinearLayout>

错误信息:

05-28 21:05:59.976: W/dalvikvm(4638): threadid=1: thread exiting with uncaught exception (group=0x4158f438)
05-28 21:05:59.986: E/AndroidRuntime(4638): FATAL EXCEPTION: main
05-28 21:05:59.986: E/AndroidRuntime(4638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Victory.myfragment/com.Victory.myfragment.Main}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.ActivityThread.access$600(ActivityThread.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.os.Handler.dispatchMessage(Handler.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.os.Looper.loop(Looper.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.ActivityThread.main(ActivityThread.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at dalvik.system.NativeStart.main(Native Method)
05-28 21:05:59.986: E/AndroidRuntime(4638): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.view.LayoutInflater.rInflate(LayoutInflater.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.view.LayoutInflater.inflate(LayoutInflater.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.view.LayoutInflater.inflate(LayoutInflater.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.view.LayoutInflater.inflate(LayoutInflater.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.Activity.setContentView(Activity.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at com.Victory.myfragment.Main.onCreate(Main.java:13)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.Activity.performCreate(Activity.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
05-28 21:05:59.986: E/AndroidRuntime(4638):     ... 12 more
05-28 21:05:59.986: E/AndroidRuntime(4638): Caused by: java.lang.IllegalStateException: Fragment com.Victory.myfragment.LeftFragment did not create a view.
05-28 21:05:59.986: E/AndroidRuntime(4638):     at android.app.Activity.onCreateView(Activity.java)

 

具体原因:

具体原因:
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
这个函数写成了OnCreateView (),细心很重要!!!

另外,出现问题一定要勤于调试代码。

查到有用的资料:
app包中的fragment和v4包中的fragment的使用的区别