Wednesday, November 25, 2009

[android-developers] Reg: Error in adding View to a ViewGroup

Hi,

I am trying to add a view to a viewgroup but I am getting the following error when I run the application

Error:
E/AndroidRuntime(  570): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Code:
public class ExampleApp extends ViewGroup{



    private LayoutParams mLAyoutParams;
    AlbumArtImageView mAlbumArtThumnail;


   static final String TAG = "ExampleApp";
  
  
   public ExampleApp(Context context) {
       super(context);
       Play_Button = new Button(context);
      // mAlbumArtThumnail = new ImageView(context);
       mAlbumArtThumnail = new AlbumArtImageView(context); // A view created to be added to the ViewGroup
       //this.mAlbumArtImage = BitmapFactory.decodeResource(getResources(),com.android.internal.R.drawable.emo_im_angel);
       this.setWillNotDraw(false);
      
   }

   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b)
   {
           // TODO Auto-generated method stub
   }

  
   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     setMeasuredDimension( 300, 40);

     this.addView(mAlbumArtThumnail);
   
   }


   private int measureWidth(int measureSpec) {
     int preferred = 150;
     return getMeasurement(measureSpec, preferred);
   }


   private int measureHeight(int measureSpec) {
     int preferred = 40;
     return getMeasurement(measureSpec, preferred);
   }


   private int getMeasurement(int measureSpec, int preferred) {

     int specSize = MeasureSpec.getSize(measureSpec);
     int measurement = 0;

     switch (MeasureSpec.getMode(measureSpec)) {
       case MeasureSpec.EXACTLY:
         // This means the width of this view has been given.
         measurement = specSize;
         break;
       case MeasureSpec.AT_MOST:
         // Take the minimum of the preferred size and what we were told to be.
         measurement = Math.max(preferred, specSize);
         break;
       default:
         measurement = preferred;
         break;
     }

     return measurement;

   }
  
   @Override
   public void onDraw(Canvas canvas)
   {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);

        canvas.drawRect(0, 0, 320, 40, paint);

   }
}

Can anyone please let me know where I am making the mistake?.

Thanks in advance for your help.

Regards,
Sathya

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate