错误:指定的子级已有父级

时间:2013-10-28 19:46:18

标签: java android layout view

在我的程序中,我在模拟器上出现此错误。 如果我在我的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已经有了父级,所以我用ViewGroup parent = (ViewGroup) mid.getParent();进行了调试,它返回null ...

有人可以带领我朝正确的方向前进吗?

以下是我的课程:

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 个答案:

答案 0 :(得分:1)

    Android会重复调用
  1. onDraw()。对onDraw()的第二次调用将抛出此异常,因为每个View已添加到父级。

  2. 也就是说,您通常不会更改onDraw()内的布局。此方法仅适用于Canvas。即使你设法通过调用removeAllViews()来修复此异常(因为它看起来你可能已经尝试过),你将强制Android在删除并重新添加视图时连续测量并再次绘制布局。 / p>

  3. 您可能实际上并不想将您的子视图添加到父级。如果要添加子视图,则应将其添加为HoneyCombView的子视图。为此,您需要扩展ViewGroup而不是View。再次,在其他地方添加子视图,而不是onDraw()

  4. 虽然向ViewGroup添加子视图会起作用,但它对性能不是很好。您添加到布局的每个子视图都会降低应用程序的速度。 (请参阅Use Fewer Views。)更好的选择是使用(例如)Canvas直接在canvas.drawPath()上绘制形状。