0

我发布我的问题是因为我已经阅读了很多主题,但没有人能很好地回答我的问题。

我将片段与 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): 致命异常: main 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): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 android. app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 android.app.ActivityThread.access$600(ActivityThread.java:153) 07-30 14:29 :28.122: main(ZygoteInit.java:562) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 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): 在 android.view.ViewGroup.addViewInner(ViewGroup.java:3339) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 android. view.ViewGroup.addView(ViewGroup.java:3210) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 android.view.ViewGroup.addView(ViewGroup.java:3155) 07-30 14:29: 28.122: E/AndroidRuntime(7548): 在 android.view.ViewGroup.addView(ViewGroup.java:3131) 07-30 14:29:28.122: E/AndroidRuntime(7548): 在 android.support.v4.app。

4

2 回答 2

0

调整

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

View view = inflater.inflate(R.layout.activity_home, null, false);
于 2014-03-19T18:04:51.953 回答
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);
}
于 2013-07-30T12:15:54.087 回答