几天来,我一直在为 Android 3.0 SDK 的 Fragments 概念苦苦挣扎,并想试试 StackOverflow ......
我有一个简单的(概念证明)应用程序:
这是我的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="200dp">
<fragment android:name="com.owentech.simplefragmentswap.staticfragment"
android:id="@+id/fragment1" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_width="fill_parent"/>
</RelativeLayout>
<FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_above="@+id/relativeLayout1"></FrameLayout>
</RelativeLayout>
我直接在 XML 中显示一个片段(静态片段),这很好用。
使用框架布局,我想在创建时添加一个 XML 片段 (fragment1)。
这是片段1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_width="match_parent" android:text="Fragment 1"></Button>
</RelativeLayout>
这是我的主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class mainActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create new fragment and transaction
Fragment newFragment = new fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
//transaction.replace(R.id.frameLayout1, newFragment);
transaction.add(R.id.frameLayout1, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
这会导致启动时强制关闭。
这是日志猫:
05-18 09:49:23.073: INFO/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.owentech.simplefragmentswap/.mainActivity }
05-18 09:49:23.313: INFO/ActivityManager(71): Start proc com.owentech.simplefragmentswap for activity com.owentech.simplefragmentswap/.mainActivity: pid=451 uid=10036 gids={1015}
05-18 09:49:23.324: DEBUG/AndroidRuntime(445): Shutting down VM
05-18 09:49:23.333: DEBUG/dalvikvm(445): Debugger has detached; object registry had 1 entries
05-18 09:49:23.404: INFO/AndroidRuntime(445): NOTE: attach of thread 'Binder Thread #3' failed
05-18 09:49:24.433: DEBUG/AndroidRuntime(451): Shutting down VM
05-18 09:49:24.433: WARN/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): FATAL EXCEPTION: main
**05-18 09:49:24.473: ERROR/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.owentech.simplefragmentswap/com.owentech.simplefragmentswap.mainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.**
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:521)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1865)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1822)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1802)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:916)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1587)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:500)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Activity.performStart(Activity.java:3781)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): ... 11 more
05-18 09:49:24.494: WARN/ActivityManager(71): Force finishing activity com.owentech.simplefragmentswap/.mainActivity
有谁知道我做错了什么,因为我直接从 developer.android.com 获取了代码
谢谢