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. 【BZOJ 4631】4631: 踩气球 (线段树)

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 316  Solved: 153 Description 六一儿童节到了, SHUX ...

  2. 【UOJ 34】 多项式乘法 (FFT)

    [题意] 给你两个多项式,请输出乘起来后的多项式. 先打一个递归版本的模板... #include<cstdio> #include<iostream> #include< ...

  3. mof提权原理及实现

    关于 mof 提权的原理其实很简单,就是利用了c:/windows/system32/wbem/mof/目录下的 nullevt.mof 文件,每分钟都会在一个特定的时间去执行一次的特性,来写入我们的 ...

  4. 新浪微博API的使用Python

    本文记录了用新浪微博官方Python SDK调用API进行开发的流程. 准备工作 申请成为开发者并创建一个应用: 首先要有一个新浪微博的账号,然后去新浪微博开放平台(http://open.weibo ...

  5. 不思议迷宫:逆向后的放置play

    前言 前置准备 目标分析 逆向加密逻辑 定位sign与key 解密luac 反编译luajit的bytecode 开启上帝模式 前言     看了fatezero的<阴阳师:一个非酋的逆向旅程& ...

  6. bzoj4753 最佳团体

    题目描述 JSOI 信息学代表队一共有 NN 名候选人,这些候选人从 11 到 NN 编号.方便起见,JYY 的编号是 00 号.每个候选人都由一位编号比他小的候选人R_iRi​ 推荐.如果 R_i ...

  7. 数组中找出最小的K个数

    题目 给出一个数组,找出K个最小的值 例如给出数组{5,2,4,3,1},给定K值3,则输出结果为{2,3,1} 程序 先给出第一个版本的程序 public static void printKNum ...

  8. IOS发送Email的两种方法

    IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. ...

  9. Apache Thrift使用简介

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.和其它RPC框架相比,它主要具有如下连个特点: 高性能. 它采用的是二进制序列化,并且用的是长 ...

  10. NAT详解 z

    http://www.cnblogs.com/beginmind/p/6380489.html 1.为什么出现了NAT? IP地址只有32位,最多只有42.9亿个地址,还要去掉保留地址.组播地址,能用 ...