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 4663】 (最小割)

    4663: Hack Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 69  Solved: 26 Description 由于 FZYZ 教学区禁止使 ...

  2. Properties Editor 中文编辑器 汉化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 在Eclipse的 [Help]-> [ Install New Software ...

  3. RE:从零开始的AGC被虐(到)生活(不能自理)

    RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...

  4. 【UOJ244】[UER7]短路

    [题目大意] (2n+1)*(2n+1)的矩形,由里到外每一层都有一个相同的值.问从左上走到右小经过的点累和的最小值. [思路] 一眼就是贪心.首先能够想到的是,权值最小的那些边要尽可能夺走,所以必定 ...

  5. Java Queue的测试

    上传图片没上去,提交的时候已经结束 代码链接

  6. python开发_difflib字符串比较

    在python的difflib中 HtmlDiff:比较后以html方法展示 我们比较的是字符串: 'hello world!' 和 'hElLO Wor2d!' 具体代码: from difflib ...

  7. 更新teaching中fdSubjectID为null的老数据

    UPDATE wkwke.tbTeachingV3 teaching SET teaching.fdSubjectID = (                    SELECT fdValue FR ...

  8. 让linux history命令显示命令的运行时间、在哪个机器运行的这个命令

    1.在/etc/profile的最后加入例如以下部分: USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g ...

  9. mysql知识点(三)

    1.表关联是可以利用两个表的索引的,如果是用子查询,至少第二次查询是没有办法使用索引的. 2.  为了给主查询提供数据而首先执行的查询被叫做子查询 3.如果WHERE子句的查询条件里使用了函数(WHE ...

  10. c#面试3(选择题)

    1.下列有关基本类的大小不正确的是 A.int类型是4个字节 B.bool类型是1个字节 C.long类型是8个字节 D.char类型是一个字节 3.有关数组说法不正确的是 A.数组的内存是分配在栈中 ...