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

本文详细介绍了如何根据不同版本的Android SDK使用Fragment,包括自定义Fragment的方法、XML布局文件中Fragment的配置方式、以及针对不同SDK版本的Activity兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方法一:如果你需要的的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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值