2013-10-28 69 views
0

在我的程序中,我的模擬器中出現此錯誤。 如果我在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)

我相信它說,中期已經有母,所以我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); 
    } 
} 
+2

你想要做什麼?你不應該在'onDraw()'中做佈局' – Simon

+0

我試圖從其他視圖構建一個視圖(由六邊形組成的蜂窩)http://depositphotos.com/1830169/stock-illustration-Honeycomb.html –

+1

然後使HoneycombView()一個ViewGroup或**繪製** onDraw()'六角形' – Simon

回答

1
  1. onDraw()搭載Android反覆調用。第二次致電onDraw()將拋出此異常,因爲每個View已被添加到父級。

  2. 這就是說,作爲一項規則,你永遠不會改變onDraw()內部的佈局。此方法僅適用於在Canvas上繪畫。即使您設法通過調用removeAllViews()來修復此異常(因爲它看起來像您可能已經嘗試過),但您會強制Android連續測量並一次又一次地繪製佈局,因爲視圖已刪除並重新添加。

  3. 您可能實際上並不想將您的子視圖添加到父項。如果您想添加子視圖,則應將它們添加爲HoneyCombView的子項。爲此,您需要擴展ViewGroup而不是View。再次,將子視圖添加到其他地方,而不是在onDraw()中。

  4. 雖然將子視圖添加到ViewGroup可以工作,但對性能不是很好。您添加到佈局的每個子視圖都會降低應用程序的速度。 (請參閱Use Fewer Views。)更好的選擇是使用(例如)canvas.drawPath()將圖形直接繪製到Canvas上。

相關問題