编写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();
}
}
其实一开始遇到这些错误的时候我这些方法都一遍一遍试过,但是记忆中有改对的时候但是运行时候还是出错,最后再次尝试运行时候才正确,所以有些时候错误不是代码的原因,要保持冷静,不要慌张,查询资料一定会解决的。
(后来查看的时候发现编写格式太乱了,当时不知道如何使用,现在是重新调整版)