1

I have the following crash when try to add view to linear layout in android.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.q8car.andriod.activity/com.technivance.q8car.view.FilterItemsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My Code is as following

ArrayList<ColorResponse> colors = Q8CarDBManager.getAllColors(FilterItemsActivity.this);
        addColor(-1, null, getString(R.string.no_color));

        if (colors != null && colors.size() > 0) {

            for (int i = 0; i < colors.size(); i++) {

                addColor(colors.get(i).getId(), colors.get(i).getHexaCode(), colors.get(i).getName());
            }
        }

        updateSelectedColor(-1);

And the addColor Method as following

private void addColor(final int colorId, final String colorHexa, final String title) {

        int padding = (int) getResources().getDimension(R.dimen.small_margin);

        if (inflatedView == null) {

            inflatedView = getLayoutInflater().inflate(R.layout.item_color, null);
        }

        if (mLayoutParam == null) {

            mLayoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            mLayoutParam.setMargins(0, 0, padding, 0);
        }

        inflatedView.setLayoutParams(mLayoutParam);

        ImageView colorImage = (ImageView)inflatedView.findViewById(R.id.color_image);
        TextView colorTitle = (TextView)inflatedView.findViewById(R.id.color_title);

        int color = -2;
        if (PhoneUtils.isEmpty(colorHexa) == false)
            color = Color.parseColor("#" + colorHexa);

        BitmapDrawable drawable = ImageManager.getInstance().getRoundedShapeWithColor(this, R.drawable.no_color, color , true);
        colorImage.setImageDrawable(drawable);

        colorImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                mColorId = colorId;
                updateSelectedColor(colorId);
            }
        });
        colorTitle.setText(title);

//      if(inflatedView.getParent()!=null)
//          ((ViewGroup)inflatedView.getParent()).removeView(inflatedView);

        inflatedView.setId(colorId);
        mColorsImageViews.put(colorId, colorImage);
        mColorsLayout.addView(inflatedView);
    }

Please not that when I create the inflated layout every time it takes a lot of time.

EDIT

this is the full stack trace

Version Name2.4.13
Version Code
23Android Version5.0.1
Devicesamsung (GT-I9505)

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.q8car.andriod.activity/com.technivance.q8car.view.FilterItemsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4212)
at android.view.ViewGroup.addView(ViewGroup.java:4065)
at android.view.ViewGroup.addView(ViewGroup.java:4010)
at android.view.ViewGroup.addView(ViewGroup.java:3986)
at com.technivance.q8car.view.FilterItemsActivity.addColor(FilterItemsActivity.java:297)
at com.technivance.q8car.view.FilterItemsActivity.initializeUIComponentsData(FilterItemsActivity.java:186)
at com.technivance.q8car.view.FilterItemsActivity.onCreate(FilterItemsActivity.java:98)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
... 10 more

and the method that create the crash is addColor [mColorsLayout.addView(mColorInflatedView);];

3
  • 1
    Please post the entire stack trace, and indicate what lines in that stack trace correspond to lines in your code listings shown above. Commented Sep 13, 2015 at 15:04
  • @CommonsWare please review my edit. Commented Sep 13, 2015 at 15:11
  • "and the method that create the crash is addColor [mColorsLayout.addView(mColorInflatedView);];" -- there is no such line in your code. Do you mean mColorsLayout.addView(inflatedView);? Commented Sep 13, 2015 at 15:13

1 Answer 1

1

The exception message pretty much tells you where the problem is. It seems like your inflatedView can only be not null when you've already attached it to mColorsLayout, and then you try to attach it again. You have to inflate every view separately instead of having a single View object declared at the class level for every color.

Sign up to request clarification or add additional context in comments.

10 Comments

It takes a lot of time and slow down the application.
Then you have to look for other means of optimization. If you want N views on the screen, all of them have to be inflated, there is no workaround.
The problem that I need to make horizontal list, so I need to add items by myself.
@AmiraElsayedIsmail: That is not going to work well. Moreover, I feel fairly confident that you cannot display views for 500 colors at once on most device screens. Use a RecyclerView or some variant of AdapterView, instead of whatever you are doing now (presumably having all 500 in a LinearLayout in a ScrollView or HorizontalScrollView). That way, you will not have to create 500 views, but only a small fraction of that, recycling them as the user scrolls.
@AmiraElsayedIsmail You just call it on the RecyclerView.Adapter that's backing your view: recyclerView.getAdapter().notifyDataSetChanged().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.