您的位置:首页 > 其它

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

2016-08-24 22:10 316 查看
编写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"><fragmentandroid: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" /><FrameLayoutandroid: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.ListFragmentpackage 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);}@Overridepublic 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时,回调该方法@Overridepublic 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中被删除时回调该方法@Overridepublic void onDetach() {super.onDetach();//讲Callbacks赋为nullmCallbacks = null;}//当用户单击某列表时激发该回调方法@Overridepublic 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_book_twopane);}@Overridepublic 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容器当前显示的fragmentgetFragmentManager().beginTransaction().replace(R.id.book_detail_container,fragment).commit();}}
其实一开始遇到这些错误的时候我这些方法都一遍一遍试过,但是记忆中有改对的时候但是运行时候还是出错,最后再次尝试运行时候才正确,所以有些时候错误不是代码的原因,要保持冷静,不要慌张,查询资料一定会解决的。(后来查看的时候发现编写格式太乱了,当时不知道如何使用,现在是重新调整版)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐