The error is in here:

public int getLeftMargin() { ViewGroup.MarginLayoutParams lp = (ViewGroup
.MarginLayoutParams) mView.getLayoutParams(); return lp.leftMargin; }

mView.getLayoutParams()
http://developer.android.com/reference/android/view/View.html#getLayoutParams()
returns a ViewGroup.LayoutParams 
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
not a ViewGroup.MarginLayoutParams

On Tuesday, October 9, 2012 6:31:23 PM UTC+1, Timothy Caraballo wrote:
>
> I've posted this question to 
> StackOverflow<http://stackoverflow.com/questions/12776557/classcastexception-of-layoutparams-to-marginlayoutparams-after-setting-a-views>,
>  
> but no answer there yet, so I thought I'd ask here as well.
>
> I've written a small proxy class so that I can use ObjectAnimator to 
> animate a view's margin. After checking that this worked and everything 
> animated properly, I wanted to adjust the view's size before the animation, 
> but after I set the width, my animation fails with a ClassCastException. 
> I haven't a clue why.
>
> private class handleClickListener implements View.OnClickListener { 
> private static final int LEFT_MARGIN_VISIBLE = 0; private static final 
> int LEFT_MARGIN_HIDDEN = -196; public void onClick(View view) { 
> LinearLayout row = (LinearLayout) view.getParent().getParent();RelativeLayout 
> options = (RelativeLayout) row
> .findViewById(R.id.options); LayoutProxy layoutProxy = new 
> LayoutProxy(options); // First make sure the options are the right width 
> to fill the screen with the handle int rowWidth = row.getWidth(); int 
> handleWidth = view.getWidth(); layoutProxy.setWidth(rowWidth - 
> handleWidth); ObjectAnimator animation; if (layoutProxy.getLeftMargin() 
> == Util.dipsToPixels(options, LEFT_MARGIN_VISIBLE)) { animation = 
> ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util.dipsToPixels(view, 
> LEFT_MARGIN_VISIBLE), Util.dipsToPixels(view, LEFT_MARGIN_HIDDEN)); } 
> else { animation = ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util
> .dipsToPixels(view, LEFT_MARGIN_HIDDEN), Util.dipsToPixels(view, 
> LEFT_MARGIN_VISIBLE)); } animation.setDuration(200); animation.start(); } /** 
> * Allows an ObjectAnimator to set/get margins and dimensions of a view 
> */private class LayoutProxy { private ViewGroup mView
> ; public LayoutProxy(View view) { mView = (ViewGroup) view; } public int 
> getWidth() { ViewGroup.LayoutParams lp = mView.getLayoutParams(); return 
> lp.width; } public void setWidth(int width) { ViewGroup.LayoutParams lp = 
> mView.getLayoutParams(); mView.setLayoutParams(new 
> ViewGroup.LayoutParams(width, 
> lp.height)); } public int getHeight() { ViewGroup.LayoutParams lp = mView
> .getLayoutParams(); return lp.height; } public void setHeight(int height) 
> { ViewGroup.LayoutParams lp = mView.getLayoutParams(); mView
> .setLayoutParams(new ViewGroup.LayoutParams(lp.width, height)); } public 
> int getLeftMargin() { ViewGroup.MarginLayoutParams lp = (ViewGroup
> .MarginLayoutParams) mView.getLayoutParams(); return lp.leftMargin; } 
> public void setLeftMargin(int margin) { MarginLayoutParams lp = 
> (MarginLayoutParams) mView.getLayoutParams(); lp.setMargins(margin, lp
> .topMargin, lp.rightMargin, lp.bottomMargin); mView.requestLayout(); } 
> public int getTopMargin() { MarginLayoutParams lp = (MarginLayoutParams) 
> mView.getLayoutParams(); return lp.topMargin; } public void 
> setTopMargin(int margin) { MarginLayoutParams lp = (MarginLayoutParams) 
> mView.getLayoutParams(); lp.setMargins(lp.leftMargin, margin, lp
> .rightMargin, lp.bottomMargin); mView.requestLayout(); } public int 
> getRightMargin() { MarginLayoutParams lp = (MarginLayoutParams) mView
> .getLayoutParams(); return lp.rightMargin; } public void 
> setRightMargin(int margin) { MarginLayoutParams lp = (MarginLayoutParams) 
> mView.getLayoutParams(); lp.setMargins(lp.leftMargin, lp.topMargin, 
> margin, lp.bottomMargin); mView.requestLayout(); } public int 
> getBottomMargin() { MarginLayoutParams lp = (MarginLayoutParams) mView
> .getLayoutParams(); return lp.bottomMargin; } public void 
> setBottomMargin(int margin) { MarginLayoutParams lp = (MarginLayoutParams) 
> mView.getLayoutParams(); lp.setMargins(lp.leftMargin, lp.topMargin, lp
> .rightMargin, margin); mView.requestLayout(); } } }
>
> And the error:
>
> FATAL EXCEPTION: main java.lang.ClassCastException: 
> android.view.ViewGroup$LayoutParams 
> cannot be cast to android.view.ViewGroup$MarginLayoutParams at com.test
> .data.ContactListCursorAdapter$handleClickListener$LayoutProxy
> .getLeftMargin(ContactListCursorAdapter.java:135) at com.test.data
> .ContactListCursorAdapter$handleClickListener.onClick
> (ContactListCursorAdapter.java:94) at android.view.View.performClick(View
> .java:4084) at android.view.View$PerformClick.run(View.java:16966)
>
> What am I doing wrong and why would setting the margins just not work 
> after setting a width?
>

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

Reply via email to