Fragment是activity的界面中的一部分或一种行为。你能够把多个Fragment们组合到一个activity中来创建一个多面界面而且你能够在多个activity中重用一个Fragment。你能够把Fragment觉得模块化的一段activity,它具有自己的生命周期,接收它自己的事件。并能够在activity执行时被加入或删除。

一个好的帖子:http://my.oschina.net/u/270164/blog/85347。

使用Fragment的包为android.app.FragmentManager,可是因为其对3.0下版本号不兼容,所以要
引入android.support.v4.app.FragmentManager 两个包对Fragment同样方法的定义有所不同,因此在import时,常常easy混淆导致程序报错。以下就来总结下:

1、编译无错误,执行时报错:android.view.InflateException: Binary XML file line #7: Error inflating class fragment

出现了上述错误,原因往往是你在某个.xml文件使用了<fragment>标签,但相应的xx.java文件里import的却是android.support.v4.app.Fragment。android.support.v4.app.Fragment:能够兼容到1.6的版本号,不能使用<fragment>标签,须要在程序中通过add或者replace的方式加入。

2、编译错误:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
    public static FragmentManager childFm;
    protected void onCreate(Bundle savedInstanceState) {
       
 super.onCreate(savedInstanceState);
       
 setContentView(R.layout.activity_main);
       
 childFm=getSupportFragmentManager();
        FragmentTransaction ft=childFm.beginTransaction();
        Frag1 f1=new Frag1();
        ft.add(f1,"第一").commit();
    }

}

代码如上。在add处提示:The
method add(Fragment, String) in thetype FragmentTransaction is not applicable for the arguments (Frag1, String)。

原因:我的Frag1.java中。import的包是 android.app.Fragment所以报错。

改成import android.support.v4.app.Fragment;就好了。

3、此外,在调用Activity时,包为 android.support.v4.app.FragmentActivity的类,都extends的是FragmentActivity而不是Activity。

android.app.FragmentManager 与 android.support.v4.app.FragmentManager带来的若干Error的更多相关文章

  1. Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

    E/AndroidRuntime﹕ FATAL EXCEPTION: mainProcess: org.example.magnusluca.drawertestapp, PID: 3624java. ...

  2. Android fragment切换后onresume时报 Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim'

    动态加载fragment以后,调用了remove方法移除Fragment,在返回来的时候报 Attempt to write to field 'int android.support.v4.app. ...

  3. android.app.Fragment与android.support.v4.app.Fragment不一致

    在看法中用到的Fragment在类型转换的时候非常easy出现这样的问题: 对照代码: class MyFragmentPagerAdapter extends FragmentPagerAdapte ...

  4. 在布局中使用android.support.v4.app.Fragment的注意事项

    1.Activity必须继承android.support.v4.app.FragmentActivity 2.fragment标签的name属性必须是完全限定包名,如下: <LinearLay ...

  5. app:transformClassesWithJarMergingForDebug uplicate entry: android/support/v4/app/BackStackState$1.class

    .Execution failed for task ':app:transformClassesWithJarMergingForDebug'.> com.android.build.api. ...

  6. android.support.v4.app.Fragment和android.app.Fragment区别

    1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...

  7. 安卓开发错误:The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.

    今天在使用低版本下的ActionBar,在继承ActionBarActivity时报了"The type Android.support.v4.app.TaskStackBuilder$Su ...

  8. [转]android.support.v4.app.Fragment和android.app.Fragment区别

      1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support ...

  9. android.support.v4.app.Fragment vs android.app.Fragment 的区别

    android.support.v4.app.Fragment vs android.app.Fragment 的区别 我开过平板相关应用,用了Fragment来处理.后来重新开发另外一个应用,直接引 ...

随机推荐

  1. 「WC2010」重建计划(长链剖分/点分治)

    「WC2010」重建计划(长链剖分/点分治) 题目描述 有一棵大小为 \(n\) 的树,给定 \(L, R\) ,要求找到一条长度在 \([L, R]\) 的路径,并且路径上边权的平均值最大 \(1 ...

  2. [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)

    题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书 ...

  3. 1.7(SQL学习笔记)游标

    一.游标简介 SELECT语句得到的是一个结果集,有时我们需要对结果集中的单条数据进行处理. 这时就需要使用游标,游标定义时和一个SELECT语句的结果集关联在一起. 游标执行这个结果集,可以在结果集 ...

  4. pat 素数对猜想

    让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素 ...

  5. 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】

    P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...

  6. 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化

    秋实大哥与小朋友 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...

  7. html5开发<video>视频字幕的程序

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="utf-8 ...

  8. 王者参考jar包

  9. 使用 VS2012 开发 IDA GUI 插件 WIN32 SDK 和 内置函数 AskUsingForm_c

    1. 执行菜单的File->New->Project… (Ctrl-Shift-N)打开新建工程窗口. 2. 展开左侧的Visual C++项目模板分支,然后选择右侧的Win32 Proj ...

  10. artDialog双击会关闭对话框的修改

    artDialog,一个jquery的对话框插件,很好用的说 但是在使用时发现鼠标双击时会自半对话框,查看源码发现有个监听鼠标双击的事件如下图: 简单的方法就是把该行去掉,为了扩展可改成如下图: 这样 ...