Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误:指定的子级已具有父级_Java_Android_Layout_View - Fatal编程技术网

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

Java 错误:指定的子级已具有父级,java,android,layout,view,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

在我的程序中,我在模拟器上遇到了这个错误如果我在我的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);
    }
}
  • onDraw()
    被Android反复调用。对
    onDraw()
    的第二次调用将引发此异常,因为每个
    视图都已添加到父视图中

  • 也就是说,作为一项规则,您永远不会更改
    onDraw()中的布局。此方法仅用于在
    画布上绘制。即使您通过调用
    removeAllViews()
    (看起来您可能已经尝试过了)来修复此异常,也会迫使Android在视图被删除和重新添加时不断地测量和绘制布局

  • 您可能实际上不想将子视图添加到父视图中。如果要添加子视图,应将其添加为
    蜂巢视图的子视图
    。为此,您需要扩展
    ViewGroup
    ,而不是
    View
    。再次,将子视图添加到其他位置,而不是
    onDraw()

  • 虽然将子视图添加到
    视图组
    会起作用,但这对性能不是很好。添加到布局中的每个子视图都会减慢应用程序的速度。(请参阅。)一个更好的选择是使用(例如)
    Canvas.drawPath()
    直接在
    Canvas
    上绘制形状


  • 你想干什么?您不应该在
    onDraw()
    中进行布局,我正在尝试从其他视图(由六边形组成的蜂巢)构建视图,然后将HoneycombView()设置为视图组,或者在
    onDraw()中绘制六边形。
    我尝试在onDraw中绘制六边形,但仍然出现错误。我将尝试查看组。但是,为什么它能在我的手机上工作而不能在模拟器上工作呢?
    onDraw()
    被Android反复调用。对
    onDraw()
    的第二次调用将引发此异常,因为每个
    视图都已添加到父视图中。