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);];
mColorsLayout.addView(inflatedView);?