使用自定义FragmentTabHost 时 程序启动失败

来源:互联网 发布:js格式化日期时间 编辑:程序博客网 时间:2024/04/28 10:16

问题背景:
我的项目中使用Android自带的FragmentTabHost + Fragment 实现了底部操作栏切换的功能,但是在界面切换的时候发现不能保存fragment的状态,通过在网上查找资料发现可以使用自定义 FragmentTabHost 来保存Fragment 的状态。但是在实际使用中发现 添加FragmentTabHost 代码后,程序不能启动了。

解决方法:
通过调试代码发现,在对FragmentTabHost 对象加载时,出现加载失败

private FragmentTabHost mTabHost;mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

然后就想到修改了代码中的FragmentTabHost 对象,但是未修改xml资源中的 FragmentTabHost 对象

修改前:

<android.support.v4.app.FragmentTabHost    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:background="@drawable/main_tab_item_bg">    <FrameLayout        android:id="@android:id/tabcontent"        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_weight="0" />            </android.support.v4.app.FragmentTabHost>

修改后:

    <com.example.fragmenttabhostdemo.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:background="@drawable/main_tab_item_bg">        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />                </com.example.fragmenttabhostdemo.FragmentTabHost>

只要将资源中的FragmentTabHost 改为自己的文件路径程序即可正常运行了。

备注:
FragmentTabHost 修改的代码网上一搜都会出来,就不在这里贴出了。

原创粉丝点击