Wednesday, May 23, 2012

what is wrong with this assignment?

I have defined a class XX like this:



   public class XX extends RelativeLayout {
protected static final boolean DEBUG = true;

public XX(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public XX(Context context, AttributeSet attrs) {
super(context, attrs);
if (DEBUG)
Log.i(this.getClass().getSimpleName(), " ->2"
+ Thread.currentThread().getStackTrace()[2].getMethodName());
//getAttributes(context, attrs);

}

public XX(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (DEBUG)
Log.i(this.getClass().getSimpleName(), " ->3"
+ Thread.currentThread().getStackTrace()[2].getMethodName());
//getAttributes(context, attrs);

}

}


and in my code I write:



RelativeLayout v = (RelativeLayout) this.findViewById(R.id.switch_TCMyTB);
XX x = (XX) v; //<----- crashes here


but it crashes with the assignment. I assume the because XX extends View I can just assign the view (RelativeLayout) to an XX object.



But it crashes. What is wrong with the assignment?



EDIT:



I changed extends View to extends RelativeLayout. Also changed View v to RelativeLayout v.
But I still get a classCastException..???? Why?



While



RelativeLayout r = (RelativeLayout) v; 


certainly works fine.





No comments:

Post a Comment