Android 原生片段与机器人支持v4库片段



>我有一个XML布局文件,它被一个活动膨胀了

<?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>

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

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

....
import android.support.v4.app.FragmentTransaction;
import roboguice.fragment.RoboListFragment;
public class TabFragment extends RoboListFragment {
.... 
....

运行时,应用崩溃,ADB 日志猫显示以下错误:

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

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

所以。。。我能做什么?我确信这就是应用程序崩溃的原因。

下面的全栈跟踪

0-25 20:59:54.535: 信息/应用程序策略(1903): 是应用程序安装启用: 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: 调试/启动器.启动器模型(2152): --> 包:com.xyz 10-25 21:00:00.050: INFO/SocialHub(6289): [UinboxReceiver] onReceive()>> intent.getData() : com.xyz 10-25 21:00:00.345:调试/启动器.启动器模型(2152):-->更新包 com.xyz 10-25 21:00:00.345: 调试/启动器.启动器模型(2152): --> 包: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|-1|0 com.sec.android.app.twlauncher.ApplicationInfo@421323f0 10-25 21:00:01.675: INFO/ActivityManager(1903): START {flg=0x10000000 cmp=com.xyz/.TabActivity} from pid 7524 10-25 21:00:01.775:INFO/ActivityManager(1903):启动活动 com.xyz/.TabActivity 的过程 com.xyz:pid=7536 uid=10017 gids={3003} java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.xyz/com.xyz.TabActivity}: android.view.InflateException: 二进制 XML 文件行 #22: 膨胀类片段时出错 at com.xyz.TabActivity.onCreate(TabActivity.java:25) 10-25 21:00:24.225:INFO/ActivityManager(1903):进程 com.xyz(pid 7536)已死亡。

使用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);
}
}

此外,请确保在尝试执行其他任何操作之前,在"活动"的onCreate()中调用super.onCreate(savedInstanceState);

我不确定,但我在所有片段中都调用了超级类,这对我有用。

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

检查这是否有效。

最新更新