Android原生片段vs roboguice支持v4库片段

时间:2012-10-26 03:21:05

标签: android android-fragments android-listfragment roboguice android-support-library

我有一个XML布局文件,它被一个Activity

夸大了
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
<LinearLayout
      android:id="@+id/content2"
      android:background="@color/lighter_gray"
      android:layout_width="match_parent"
      android:orientation="horizontal"
      android:layout_height="match_parent" >

<fragment class="com.xyz.fragments.TabFragment"
          android:id="@+id/tabs"
  android:layout_weight="1"
  android:layout_width="0px"
  android:layout_height="match_parent"
  />
<FrameLayout
  android:id="@+id/fragment_holder"
  android:layout_weight="2"
  android:layout_width="0px"
  android:layout_height="match_parent" />

  </LinearLayout>
</RelativeLayout>

Activity是FragmentActivity的子类(非直接)(来自v4库)。

现在,在com.xyz.fragments.TabFragment中,我有以下类声明

....
import android.support.v4.app.FragmentTransaction;
import roboguice.fragment.RoboListFragment;

public class TabFragment extends RoboListFragment {

.... 
....

运行时,应用程序崩溃,adb logcat显示以下错误:

java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.xyz/com.xyz.xxActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class fragment

所以第22行恰好是xml布局中的这一行

fragment class="com.xyz.fragments.TabFragment"

它是红色下划线......它说      TabFragment不能分配给android.app.fragment

好吧我明白了,我正在使用来自roboguice的FragmentList,它来自支持库v4,与android.app.fragment不同

所以...我该怎么办?而且我很乐意这就是应用程序崩溃的原因。

下面的完整堆栈跟踪

0-25 20:59:54.535:INFO / ApplicationPolicy(1903):isApplicationInstallationEnabled:pkg com.xyz 10-25 20:59:55.455:INFO / PackageManager(1903):删除非系统包:com.xyz 10-25 20:59:55.455:INFO / ActivityManager(1903):强制停止包com.xyz uid = 10017 10-25 20:59:55.610:INFO / PackageManager(1903):ICS_DEBUG scanPackageLI进入com.xyz 10-25 20:59:55.610:INFO / PackageManager(1903):ICS_DEBUG检查com.xyz 10-25 20:59:55.615:INFO / PackageManager(1903):运行dexopt on:com.xyz 10-25 20:59:58.390:INFO / ActivityManager(1903):强制停止包com.xyz uid = 10017 10-25 20:59:59.305:DEBUG / PackageManager(1903):安装在/data/app/com.xyz-2.apk中的新软件包 10-25 20:59:59.705:INFO / ActivityManager(1903):强制停止包com.xyz uid = 10017 10-25 20:59:59.875:DEBUG / Launcher.LauncherModel(2152): - &gt;包:com.xyz 10-25 21:00:00.050:INFO / SocialHub(6289):[UinboxReceiver] onReceive()&gt;&gt; intent.getData():com.xyz 10-25 21:00:00.345:DEBUG / Launcher.LauncherModel(2152): - &gt;更新包com.xyz 10-25 21:00:00.345:DEBUG / Launcher.LauncherModel(2152): - &gt;包:com.xyz 10-25 21:00:00.640:INFO / DebugDb(2152):更新应用信息-1 com.sec.android.app.twlauncher.ApplicationInfo xyz -1 4 15 75 | -1 | -1 | -1 | -1 | 0 com.sec.android.app.twlauncher.ApplicationInfo@421323f0 10-25 21:00:01.675:INFO / ActivityManager(1903):从pid 7524开始{flg = 0x10000000 cmp = com.xyz / .TabActivity} 10-25 21:00:01.775:INFO / ActivityManager(1903):启动proc com.xyz以获取活动com.xyz / .TabActivity:pid = 7536 uid = 10017 gids = {3003}         java.lang.RuntimeException:无法启动活动ComponentInfo {com.xyz / com.xyz.TabActivity}:android.view.InflateException:二进制XML文件行#22:错误膨胀类片段         在com.xyz.TabActivity.onCreate(TabActivity.java:25) 10-25 21:00:24.225:INFO / ActivityManager(1903):进程com.xyz(pid 7536)已经死亡。

2 个答案:

答案 0 :(得分:1)

使用name而不是class,如此:

<fragment android:name="com.cyz.fragments.TabFragment"
        android:id="@+id/fragment_tab"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

确保片段附加到的活动扩展为RoboFragmentActivity

确保覆盖片段中的onCreateView

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

另外,在尝试执行任何其他操作之前,请务必在活动super.onCreate(savedInstanceState);中致电onCreate()

答案 1 :(得分:0)

我不确定,但我在所有片段中都称超级课程,这对我有用。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.my_fragment, container, false);
}

检查是否有效。