指定的子项已经有一个父项,首先在子项的父项上删除View()

时间:2013-07-30 12:13:12

标签: android android-layout android-fragments

我发布了我的问题,因为我已经阅读了很多主题,但没有人对我的问题有一个很好的答案。

我使用片段和Android的滑动菜单库。

这是我的代码:

我的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null)
        this.kaza = savedInstanceState.getString("kaza");

    View view = inflater.inflate(R.layout.activity_home, container, false);

    //Construction de l'ImageView
    FrameLayout frameLayout = (FrameLayout) getActivity().findViewById(R.id.PhotoPrincipale);
    FrameLayout.LayoutParams layoutparams=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
    frameLayout.setLayoutParams(layoutparams);
    ImageView v = new ImageView(getActivity());

    //Récupération des IDS des kazas
    try {
        ListsID = ReadData(context);
    } catch (FileNotFoundException e){
        e.printStackTrace();
    }
    String iduser = (String) ListsID.get(0);
    String token = (String) ListsID.get(1);
    String idkaza = (String) ListsID.get(2);

    RecupPhotos(iduser, token, idkaza);

    for (int j = 0; j < URLBig.size(); j++) {
        new DownloadImageTask(v).execute(URLBig.get(j));
        frameLayout.addView(v,j);
    }

    return frameLayout;
}

我的活动

public class FragmentChangeActivity extends BaseActivity {

private Fragment mContent;

public FragmentChangeActivity() {
    super(R.string.changing_fragments);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set the Above View
    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
    if (mContent == null)
        mContent = new KazaFragment("Gambetta");    

    System.out.println(mContent);

    // set the Above View
    setContentView(R.layout.activity_home);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.PhotoPrincipale, mContent)
    .commit();

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame, new ColorMenuFragment())
    .commit();

    // customize the SlidingMenu
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}

public void switchContent(Fragment fragment) {
    mContent = fragment;
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, fragment)
    .commit();
    getSlidingMenu().showContent();
}

}

和我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity" >

<ImageButton
    android:id="@+id/Actu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"
    android:cropToPadding="false"
    android:src="@drawable/actu" />

<ImageButton
    android:id="@+id/PagePhotos"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toStartOf="@id/Actu"
    android:background="@android:color/transparent"
    android:cropToPadding="false"
    android:src="@drawable/photos" />

<FrameLayout
    android:id="@+id/PhotoPrincipale"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:layout_below="@+id/Actu"
    android:layout_margin="2dp"
    android:background="@drawable/shapeinscription"
    android:padding="2dp" />

所以,我搜索在FrameLaout“PhotoPrincipale”中一个接一个地显示5张图片 但一直都是同样的错误。

  

指定的孩子已经有父母。您必须首先在孩子的父母上调用removeView()

我完全迷失了:(。

非常感谢你的帮助

  
    

07-30 14:29:28.122:E / AndroidRuntime(7548):致命异常:主要     07-30 14:29:28.122:E / AndroidRuntime(7548):     java.lang.RuntimeException:无法启动活动     ComponentInfo {com.jeremyfeinstein.slidingmenu.example / com.jeremyfeinstein.slidingmenu.example.fragments.FragmentChangeActivity}:     java.lang.IllegalStateException:指定的子节点已经有了     家长。您必须首先在孩子的父母上调用removeView()。 07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.app.ActivityThread.access $ 600(ActivityThread.java:153)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.os.Handler.dispatchMessage(Handler.java:99)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.os.Looper.loop(Looper.java:137)07-30 14:29:28.122:     E / AndroidRuntime(7548):at     android.app.ActivityThread.main(ActivityThread.java:5227)07-30     14:29:28.122:E / AndroidRuntime(7548):at     java.lang.reflect.Method.invokeNative(Native Method)07-30     14:29:28.122:E / AndroidRuntime(7548):at     java.lang.reflect.Method.invoke(Method.java:511)07-30 14:29:28.122:     E / AndroidRuntime(7548):at     com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)07-30     14:29:28.122:E / AndroidRuntime(7548):at     dalvik.system.NativeStart.main(Native Method)07-30 14:29:28.122:     E / AndroidRuntime(7548):引起:java.lang.IllegalStateException:     指定的子项已有父项。你必须调用removeView()     先关于孩子的父母。 07-30 14:29:28.122:     E / AndroidRuntime(7548):at     android.view.ViewGroup.addViewInner(ViewGroup.java:3339)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.view.ViewGroup.addView(ViewGroup.java:3210)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.view.ViewGroup.addView(ViewGroup.java:3155)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.view.ViewGroup.addView(ViewGroup.java:3131)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:874)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)     07-30 14:29:28.122:E / AndroidRuntime(7548):at     android.app.Activity.performStart(Activity.java:5114)07-30     14:29:28.122:E / AndroidRuntime(7548):at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)

  

2 个答案:

答案 0 :(得分:0)

您要将URLBig.size()次添加到相同的ImageView中。这是错误的,因为异常暗示

for (int j = 0; j < URLBig.size(); j++) {
    ImageView v = new ImageView(getActivity());
    new DownloadImageTask(v).execute(URLBig.get(j));
    frameLayout.addView(v,j);
}

答案 1 :(得分:0)

修改

View view = inflater.inflate(R.layout.activity_home, container, false);

View view = inflater.inflate(R.layout.activity_home, null, false);