【发布时间】:2015-09-13 14:55:34
【问题描述】:
尝试在 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.
我的代码如下
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);
而addColor方法如下
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);
}
请不要这样,当我每次创建膨胀布局时都会花费很多时间。
编辑
这是完整的堆栈跟踪
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
造成崩溃的方法是addColor [mColorsLayout.addView(mColorInflatedView);];
【问题讨论】:
-
请发布整个堆栈跟踪,并指出该堆栈跟踪中的哪些行对应于上面显示的代码清单中的行。
-
@CommonsWare 请检查我的编辑。
-
"和造成崩溃的方法是 addColor [mColorsLayout.addView(mColorInflatedView);];" -- 你的代码中没有这样的行。你的意思是
mColorsLayout.addView(inflatedView);?
标签: android android-linearlayout layout-inflater android-inflate