0

I'm not sure why this is happening but I'm getting an error stating: Calling layout.removeAllViews(); still results in IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

The strange part is I've called: removeAllViews(); before adding a new one:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download);

...

    ImageView imageViewz = (ImageView) findViewById(R.id.imageView6); 
            Picasso.with(context).load(background).into(imageViewz);

LinearLayout layout = new LinearLayout(Download.this);
                layout.setId(R.id.download);
                LayoutParams layoutParams 
                 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                layout.setLayoutParams(layoutParams);
                layout.setOrientation(LinearLayout.VERTICAL);
                LayoutParams imageViewLayoutParams 
                 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                imageViewz.setLayoutParams(imageViewLayoutParams);
                layout.removeAllViews();
                layout.addView(imageViewz);
                setContentView(layout);

Yet I still get the fatal error...so I'm not sure exactly why this is happening.

Any suggestions are appreciated.

  • Please post your entire stack trace. My guess is that your problem is not with `layout`, but rather with `imageViewz`. – CommonsWare Aug 06 '15 at 20:45
  • Thank you @CommonsWare - Let me know if this is enough detail for you - http://pastebin.com/4uWdz7Zq (BTW line 154 is layout.addView(imageViewz);) – AndroidAndroidAndroid Aug 06 '15 at 20:47

1 Answers1

1

Your problem is not with layout. Your problem is with imageViewz. It already has a parent, and that is what is triggering your exception. You need to remove imageViewz from its current parent before you add it to layout.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I truly appreciate the insight @CommonsWare. I tried as you suggested - however I'm still getting the same error on the same line.... I tried 3 implementations - none seem to be working - mind taking a look for a second? http://pastebin.com/fhEMG2Ug – AndroidAndroidAndroid Aug 07 '15 at 13:15
  • @NoobDev910: You are attempting to remove `imageViewz` from `layout`. You just created `layout` a few lines earlier. You know that `layout` is not `imageViewz`'s current parent. – CommonsWare Aug 07 '15 at 13:24
  • Got it. (I appreciate the help) Just out of curiosity... shouldn't this resolve this issue? layout.removeAllViews(); layout.addView(imageViewz); setContentView(layout); (It's still crashing at the same point) – AndroidAndroidAndroid Aug 07 '15 at 13:54
  • @NoobDev910: "shouldn't this resolve this issue?" -- no. `imageViewz` has a parent. **That parent is not `layout`**, because you just created the `layout` object. Hence, calling `removeAllViews()` on `layout` **will have no effect on `imageViewz`**. *You* were the one who put `imageViewz` in another parent. *You* are the one who has to determine what that parent is (e.g., call `getParent()` on `imageViewz`). And *you* are the one who has to the *that parent* (which, I repeat, is not your newly-created `layout`) to remove `imageViewz` from its list of children. – CommonsWare Aug 07 '15 at 13:58