我已經定義一個類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(...); ?我以前沒有做的自定義組件,因此它只是想沿着你的問題。 –