【发布时间】:2011-08-03 20:43:23
【问题描述】:
我正在尝试使用 Android 兼容性包来创建一个使用 Fragments 的向后兼容的应用程序。但是,当我在 Android v2.2 模拟器上运行它时它会崩溃。它不会在我的 Xoom (v3.2) 上崩溃。我怀疑 main.xml 中的片段标签可能是原因:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment android:name="com.companyname.appname.MainMenuFragment"
android:id="@+id/mainMenu"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
这是 FragmentActivity:
package com.companyname.appname;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class AppName extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这是片段:
package com.companyname.appname;
import android.support.v4.app.Fragment;
public class MainMenuFragment extends Fragment {
}
有什么想法吗?
谢谢
编辑:我的目标是 API 级别 8 (Android v2.2)
【问题讨论】:
-
从 logcat 发布堆栈跟踪
-
如果您希望我们帮助您解决崩溃问题,请发布堆栈跟踪。
-
谢谢 smith324 和 LeffelMania。错误 logcat 显示此错误:08-03 22:03:22.946: ERROR/AndroidRuntime(938): Caused by: java.lang.IllegalStateException: Fragment com.companyname.appname.MainMenuFragment 未创建视图。 所以我在 MainMenuFragment 类中覆盖了
onCreateView()并让它返回一个视图,这很有效。奇怪的是它在 v3.2 中没有崩溃。 -
当我将片段块放入布局文件中时发生了这个错误,但没有通过 FragmentTransaction 添加它。为了解决这个问题,我从布局文件中删除了片段块,并在必要时使用了 FragmentTransaction。
标签: java android android-compatibility