2013-08-28 68 views
0

我正在做畫廊的應用程序和使用下面的代碼添加圖像按鈕動態,這個工作對我的設備之一,但另一方面我得到一個添加視圖使「孩子已經有父」的錯誤

java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
E/AndroidRuntime(3358):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3381) 
E/AndroidRuntime(3358):  at android.view.ViewGroup.addView(ViewGroup.java:3252) 
E/AndroidRuntime(3358):  at android.view.ViewGroup.addView(ViewGroup.java:3197) 
E/AndroidRuntime(3358):  at android.view.ViewGroup.addView(ViewGroup.java:3173) 
E/AndroidRuntime(3358):  at package.MainActivity.onCreate(MainActivity.java:74) 

錯誤,但在其他設備上完全沒有問題,它運行良好。它運行的是運行4.1.2,它崩潰的運行4.1.1會是這樣嗎?該項目的分鐘和有針對性的SDK是16個

setContentView(R.layout.activity_main); 

    // Hook up clicks on the thumbnail views. 


    imgHolder = (LinearLayout)findViewById(R.id.imgHolder); 

    for(int x = 0; x < files.length; x++) 
    { 
     Log.d("Conor",files[x].getAbsolutePath()); 
     Drawable img = Drawable.createFromPath(files[x].getAbsolutePath()); 
     imgs.add(img); 
     thumbs.add(new ImageButtons(this, files[x].getAbsolutePath(),x)); 
     thumbs.get(x).setImageDrawable(img); 
     thumbs.get(x).setOnClickListener(onCl); 
     imgHolder.addView(thumbs.get(x)); 
    } 
    // Retrieve and cache the system's default "short" animation time. 
    mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime); 

ImageButtons類thumbs

import java.io.IOException; 

import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.util.Xml; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageButton; 

public class ImageButtons extends ImageButton{ 


    Resources res = getResources(); 


    public ImageButtons(Context context,String img,int id) 
    { 
     super(context); 
     XmlPullParser parser = res.getXml(R.layout.imagebuttons); 
     AttributeSet attributes = null; 
     int state = 0; 
     while(state != XmlPullParser.END_DOCUMENT) 
     { 
      try { 
       state = parser.next(); 
      } catch (XmlPullParserException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      }  
      if (state == XmlPullParser.START_TAG) { 
       if (parser.getName().equals("ImageButton")) { 
        attributes = Xml.asAttributeSet(parser); 
        break; 
       } 
      } 
     } 

     setId(id); 

     setLayoutParams(new LayoutParams(context, attributes)); 
    } 


} 

大拇指一個`ArrayList的是一種ArrayList<ImageButton> 和線74是指`imgHolder.addView(thumbs.get( X));

再次我的問題是,爲什麼這個工程上的一些設備,但不是別人

+2

請張貼整個logcat的堆棧跟蹤,並確保我們能說出哪行你的代碼是拋出的異常。我們還需要查看關於'thumbs'的信息,因爲看起來問題在於您試圖將'thumbs.get(x)'附加到多個父母。完全可能的差異不是Android,而是運行時環境。 – chrylis

+0

我已經添加了logcat堆棧跟蹤,所討論的行是imgHolder.addView(thumbs.get(x)) – Cob50nm

+0

是的,但是「thumbs」的值類型是什麼?你正在假設哪些代碼很重要,而且你沒有提供我們需要的東西。 – chrylis

回答

3

您可能需要一些額外的處理:

View v = thumbs.get(x); 
ViewGroup p = (ViewGroup)v.getParent(); 
p.removeView(v); 
imgHolder.addView(v); 
+0

這就是它,但我需要添加一個嘗試在它未發生的時間周圍 – Cob50nm

相關問題