在自定義控制元件的過程中犯下的錯,在呼叫有三個引數的構造方法時this(context,null,0);第二個引數為空,在Activity中 findViewById找到對應的控制元件時
阿新 • • 發佈:2019-02-05
在自定義控制元件的過程中犯下的錯,在呼叫有三個引數的構造方法時this(context,null,0);第二個引數為空,在Activity中 findViewById找到對應的控制元件,在給控制元件設定點選事件的時候一直報空指標異常。這個問題一直困擾了好長的時間,今天終於在網上找到了答案。
this(context,attrs,0);把屬性值都寫空了
public CustomSwitch(Context context, AttributeSet attrs) { this(context,attrs,0); //this(context,null,0);錯誤就出現在這裡,坑爹啊 } public CustomSwitch(Context context) { this(context,null); } public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); }
小夥伴們,解決問題的那一刻,老夫是相當的激動,覺得自己怎麼可以那麼牛!