我已经定义一个类XX这样的:这个任务有什么问题?
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);
}
}
,并在我的代码我写:
RelativeLayout v = (RelativeLayout) this.findViewById(R.id.switch_TCMyTB);
XX x = (XX) v; //<----- crashes here
,但它与分配崩溃。我假设因为XX扩展视图我可以将视图(RelativeLayout)分配给XX对象。
但它崩溃。这项任务有什么问题?
编辑:
我改变extends View
到extends RelativeLayout
。还将View v
更改为RelativeLayout v
。 但我仍然得到classCastException .. ????为什么?
虽然
RelativeLayout r = (RelativeLayout) v;
肯定能正常工作。
什么样的碰撞? ClasscastException?你能否告诉我们发生了什么? – Gomoku7
你设置你的活动内容有何看法? 哪会崩溃吗?等级豁免?你可以发布你的布局文件(excerp?) – Fabian
我不确定这是否会工作,但你有没有试过XX x =(XX)this.findViewById(...); ?我以前没有做的自定义组件,因此它只是想沿着你的问题。 –