关于添加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();}}其实一开始遇到这些错误的时候我这些方法都一遍一遍试过,但是记忆中有改对的时候但是运行时候还是出错,最后再次尝试运行时候才正确,所以有些时候错误不是代码的原因,要保持冷静,不要慌张,查询资料一定会解决的。(后来查看的时候发现编写格式太乱了,当时不知道如何使用,现在是重新调整版)
相关文章推荐
- android信息从联系人添加收件人,在联系人多选界面(联系人中有Google账户)搜索时,搜索结果出现排序错误的问题
- 关于在一个子窗口中添加的navigation中添加button出现的错误。
- 关于Myeclipse10中建立web工程添加spring配置文件出现红叉错误提示的问题
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- 关于ASP程序运行出现ADODB.Stream 错误 800a0bbc 写入文件失败 的解决办法!
- +关于vs2003下创建ASP.NET程序时出现的错误+
- 在添加windows应用程序时出现“automation服务器不能创建对象 ”的错误
- 关于在vs.net中的.htm里面添加连接,就会出现下载.ASPX文件的提示?
- 关于夏昕的spring mvc框架中出现的错误的总结
- 关于ASP程序运行出现ADODB.Stream 错误 800a0bbc 写入文件失败 的解决办法!
- 关于多次刷新网页(该网页调用COM)出现在COM中的错误
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- 在web host 的DotNetNuke中添加连接时出现的错误及解决办法
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- 关于IBM笔记本开机出现ibmpmsvc.exe应用程序错误的解决方法
- 关于ASP程序运行出现ADODB.Stream 错误 800a0bbc 写入文件失败 的解决办法!
- 关于多次刷新网页(该网页调用COM)出现在COM中的错误
- 投票模块在进行添加模块操作出现错误:
- 关于vs重新生成出现“错误 CS0006: 未能找到元数据文件”.
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法