2

ここ数日、Android 3.0 SDK のフラグメントの概念に苦労しており、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 に 1 つのフラグメント (staticfragment) を直接表示すると、問題なく動作します。

フレームレイアウトを使用して、作成時に 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 から直接コードを取得したので、誰かが私が間違っていることを知っていますか?

ありがとう

4

1 に答える 1

3

申し訳ありませんが、答えを見つけました。私のfragment1.javaでビューを膨らませると、ルートに接続されました。最後のフラグを false に変更しました。

于 2011-05-19T06:44:51.613 に答える