【发布时间】:2011-06-21 01:20:15
【问题描述】:
我对 Android 有点陌生,对 Fragments 也很陌生。我正在尝试将应用程序从活动移植到 Honeycomb 的片段(嗯,活动 + 片段)。在我们进入平板电脑屏幕之前,我们希望我们的应用程序能够与 1.6+ 设备的片段兼容性库一起工作。我已阅读 Google 的文档和示例,没有明确说明我可以或不能执行以下操作,但它对我不起作用。
移植的第一个屏幕是启动器屏幕,它有一个名为 fragment_splash.xml 的布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash_background">
<fragment class="...StartupFragmentLayout$StartupFragment"
android:id="@+id/fragStartup"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/debugText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="DEBUG ON"
android:gravity="center"
android:textSize="35sp"
android:textColor="@color/blue_text"
android:textStyle="bold"
android:shadowColor="#FFFFFF"
android:shadowRadius="2"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="bottom">
...
</fragment>
</LinearLayout>
但是,这不起作用。我收到一条错误消息,指出 StartupFragmentLayout$StartupFragment 没有创建视图。我开始怀疑我不能在<fragment> 中嵌套任何东西,因为我没有在任何文档或示例中看到它。这是正确的,还是我的问题是别的?
【问题讨论】:
-
既然您提到您正在使用 Fragment Compatibility Library,首先要检查的是您尝试用于启动屏幕的 Activity 是从 FragmentActivity 扩展而来的。如果是这种情况,并且您仍然收到错误,您能否发布确切的错误消息和/或堆栈跟踪?
-
是的,我正在使用片段兼容性库。错误说“StartupFragmentLayout$StartupFragment 没有创建视图”,它发生在 setContentView(R.layout.fragment_splash);在 OnCreate 中
-
在这种情况下,我会在下面查看 PJL 的回复 - 它似乎更符合 Fragments 的预期工作方式。
标签: android android-3.0-honeycomb android-fragments