在进行Android开发学习过程中,很容易就要自己做一个自定义的view,那么对于自定义的View,创建之后很可能就直接崩溃,报错如下所示:
Binary XML file line #19: Error inflating class com.***
那么怎么能确保不崩溃退出呢
public class YourView extends View {
// 一个参数的构造方法,在代码中创建该控件时,调用该构造方法
public YourView(Context context) {
super(context);
init();
}
// 在xml 中引用该控件时,调用该方法。attrs是定义在xml布局中的属性集合
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
}
按照对视图的创建,一般分成两种,一种是直接在代码中创建,那就一定要有第一个方法,另外一种就是在xml中进行引用,那就一定要有第二个方法。
有了上面的两个方法,咱们就可以确保正常创建加载你所创建的视图了。
网友评论