gpt4 book ai didi

java - 错误: The specified child already has a parent

转载 作者:行者123 更新时间:2023-12-01 13:55:42 25 4
gpt4 key购买 nike

在我的程序中,我的模拟器上出现此错误。 如果我在 Razr MAXX 上运行它,它可以工作吗?它说

10-28 19:38:27.935: E/AndroidRuntime(2268): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

错误发生在这一行:layout.addView(mid)

我相信它说 mid 已经有一个父级,所以我用 ViewGroupparent = (ViewGroup) mid.getParent(); 进行调试,它返回 null...

有人可以引导我走向正确的方向吗?

下面是我的类(class):

package com.example.draganddroptest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* @author Brandon Ling
*
*/
public class HoneycombView extends View {
private Context context;
private ViewGroup layout;
private HexagonView top;
private HexagonView topRight;
private HexagonView topLeft;
private HexagonView bot;
private HexagonView botLeft;
private HexagonView botRight;
private HexagonView mid;
private int radius;
private PointF shift;
private String color;
private String[] colors;
private int strokeWidth;

Paint paint = new Paint();

/**
* @param context
* @param layout
*/
public HoneycombView(Context context, AttributeSet attrs) {
this(context, null, 0, null, null, attrs, 0);
}

public HoneycombView(Context context, ViewGroup layout, int radius, PointF shift, String color, AttributeSet attrs, int strokeWidth) {
super(context);
this.context = context;

if (attrs != null) { // probably sent via xml
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HoneycombView);
this.radius = a.getInt(R.styleable.HoneycombView_radius, this.strokeWidth);
this.shift = new PointF(a.getInt(R.styleable.HoneycombView_shiftX, 0), a.getInt(
R.styleable.HoneycombView_shiftY, 0));
this.colors = a.getString(R.styleable.HoneycombView_hexColors).split("\\|");
a.recycle();
} else {
this.layout = layout;
this.radius = radius;
this.shift = shift;
this.color = color;
}
this.mid = new HexagonView(this.context, null, this.radius, this.shift, this.colors[0], 10);
this.top = mid.addHexTop(colors[1]);
this.topRight = mid.addHexTopRight(colors[2]);
this.topLeft = mid.addHexTopLeft(colors[3]);
this.bot = mid.addHexBot(colors[4]);
this.botRight = mid.addHexBotRight(colors[5]);
this.botLeft = mid.addHexBotLeft(colors[6]);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.layout = (ViewGroup) this.getParent();
ViewGroup parent = (ViewGroup) mid.getParent();
//parent.removeAllViews();
layout.addView(mid);
layout.addView(top);
layout.addView(topRight);
layout.addView(topLeft);
layout.addView(bot);
layout.addView(botRight);
layout.addView(botLeft);


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = (int) Math.ceil(this.bot.getMaxY()) + this.strokeWidth;
int width = (int) Math.ceil(this.botRight.getMaxX()) + this.strokeWidth;
setMeasuredDimension(width, height);
}
}

最佳答案

  1. onDraw() 由 Android 重复调用。第二次调用 onDraw() 将引发此异常,因为每个 View 已添加到父级。

  2. 也就是说,通常来说,您永远不会更改 onDraw() 内的布局。此方法仅用于在 Canvas 上绘画。即使您设法通过调用 removeAllViews() 来修复此异常(看起来您可能已经尝试过),您也将迫使 Android 在 View 被删除和删除时不断地一次又一次地测量和绘制布局。重新添加。

  3. 您可能实际上并不希望将 subview 添加到父 View 中。如果要添加 subview ,则应将它们添加为 HoneyCombView 的 subview 。为此,您需要扩展 ViewGroup 而不是 View。再次,将 subview 添加到其他位置,而不是在 onDraw() 中。

  4. 虽然将 subview 添加到 ViewGroup 可以,但它对性能不太好。添加到布局中的每个 subview 都会减慢应用程序的速度。 (参见 Use Fewer Views 。)更好的替代方法是使用(例如)canvas.drawPath() 将形状直接绘制到 Canvas 上。

    <

关于java - 错误: The specified child already has a parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com