android.view.InflateException: Binary XML file line #x: Error inflating class Fragment

来源:互联网 发布:美国计算机硕士 知乎 编辑:程序博客网 时间:2024/05/21 11:08

解决方法一:如果你需要的的android:minSdkVersion大于等于11

1.1首先根据你的自定义Fragment来

    import android.app.Fragment;

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    public class DitalFragment extends Fragment
    {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// TODO Auto-generated method stub
return inflater.inflate(R.layout.test_fragment_ditel, container,false);

    }

    如果你是import android.app.Fragment包,f那么你的android:minSdkVersion必须大于等于11。android:minSdkVersion="11"

1.2注意的第二点,fragment的id必不可少,android:name的自定义的fragment的路径必须正确

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
         <fragment 
            android:id="@+id/frag_tital"
            android:name="com.tk.abctest.ui.TitalFragment"

            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="8"
            /> 
         <fragment 
            android:id="@+id/frag_dital"
            android:name="com.tk.abctest.ui.DitalFragment"

            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />    
    </LinearLayout>

1.3注意的第三点,对于import android.app.Fragment包的activity则没有要求可以为FragmentActivity,或者Activity

1.4 当然还有mainifest的android:minSdkVersion="4"


解决方法二:如果你需要的的android:minSdkVersion大于等于4

    2.1对于自定义fragment 需要import android.support.v4.app.Fragment;

    2.2 同上fragment的id必不可少,android:name的自定义的fragment的路径必须正确   

    2.3 对于import android.support.v4.app.Fragment包的activity则必须为FragmentActivity,不能是Activity

    2.4 当然还有mainifest的android:minSdkVersion="11"

0 0