关于添加Fragment时出现界面错误

在开发Android应用时,遇到Fragment错误:'Error inflating class fragment'。解决办法包括在XML中正确指定fragment的android:name属性,确保使用正确的Fragment导入。在反复调试后,发现有时错误并非代码问题,保持冷静并持续查阅资料是解决问题的关键。

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



编写Fragment相关程序的时候出现如下错误(调试了好长时间,看了好多文章才解决掉,所以写此文章记录一下。

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

原因有以下几方面:

1.在XML中定义fragment时,需添加android:name="com.example.bishe.fragmenttest.bookFragment.BookListFragment“


<?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:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle">

    <fragment
        android:name="com.example.bishe.fragmenttest.bookFragment.BookListFragment"
        android:id="@+id/book_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/book_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

    </FrameLayout>
</LinearLayout>

2.在继承Fragment时需导入importandroid.app.ListFragment;而不是导入android.support.v4.app.ListFragment


package com.example.bishe.fragmenttest.bookFragment;

import android.app.Activity;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by LYLK on 2016/8/23.
 */
public class BookListFragment extends ListFragment {
    private Callbacks mCallbacks;
    public interface Callbacks{
        public void onItemSelected(Integer id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),android.R.layout.simple_list_item_activated_1,
                android.R.id.text1, BookContent.ITEMS));
    }

    //当该Fragment被添加显示到Activity时,回调该方法

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //如果Activity没有实现Callbacks接口,抛出异常
        if(!(activity instanceof  Callbacks)){
            throw new IllegalStateException(
                    "BookListFragment所在的Activity必须实现Callbacks接口!");
        }
        //把该Activity当成Callbacks对象
        mCallbacks = (Callbacks) activity;
    }

    //当该Fragment从它所从属的Activity中被删除时回调该方法

    @Override
    public void onDetach() {
        super.onDetach();
        //讲Callbacks赋为null
        mCallbacks = null;
    }

    //当用户单击某列表时激发该回调方法

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //激发mCallbacks的onItemSelected方法
        mCallbacks.onItemSelected(BookContent.ITEMS.get(position).id);
    }

    public void setActivateOnItemClick(boolean activateOnItemClick){
        getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
    }
}


此处为Activity

package com.example.bishe.fragmenttest.bookFragment;

import android.app.Activity;
import android.os.Bundle;

import com.example.bishe.fragmenttest.R;


/**
 * Created by LYLK on 2016/8/23.
 */
public class TestActivity extends Activity implements BookListFragment.Callbacks {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_twopane);
    }

    @Override
    public void onItemSelected(Integer id) {
        Bundle arguments = new Bundle();
        arguments.putInt(BookDetailFragment.ITEM_ID,id);
        BookDetailFragment fragment = new BookDetailFragment();
        //向Fragment传入参数
        fragment.setArguments(arguments);
        //使用fragment替换掉book_detail_container容器当前显示的fragment
        getFragmentManager().beginTransaction().replace(R.id.book_detail_container,fragment).commit();
    }
}

其实一开始遇到这些错误的时候我这些方法都一遍一遍试过,但是记忆中有改对的时候但是运行时候还是出错,最后再次尝试运行时候才正确,所以有些时候错误不是代码的原因,要保持冷静,不要慌张,查询资料一定会解决的。

(后来查看的时候发现编写格式太乱了,当时不知道如何使用,现在是重新调整版)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值