Android Custom View NullPointerException
来源:互联网 发布:java 接口 静态块 编辑:程序博客网 时间:2024/05/22 04:34
今天测试,发现自己写的自定义控件通过findViewById(R.id.data_ad_banner);
获取不到,为null
报错如下
最终发现是因为,在自定义控件中,设置了id
,去掉就可以了。
这里总结了一些findViewById
是Null
的常见解决方案:
1、必须实现以下方法。
public View(Context context, AttributeSet attrs) { super(context, attrs); } public View(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
2、A.findViewById(B)
,判断控件(id为B)是否在A布局里面。
3、在自定义View中,不要调用setId(int id)
方法给View设置id
。
0 0
- Android Custom View NullPointerException
- Android custom view
- android custom view
- Android - Custom View
- android : custom view
- android 之 custom view(一)
- Android Custom View (自定义 View)
- 如何解决NullPointerException in android.view.GLES20RecordingCanvas
- Android studio中Custom View使用方法
- Android Custom view —- bottom sheet
- Android Custom View---Floating Action Button Menu
- Android Custom View --- Circular(环形条)
- Android Custom View ---->invalidate() 、postInvalidate() and requestLayout()
- Android Custom View --- 水波纹效果
- Android studio中Custom View使用方法
- custom view
- Custom View
- java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Vi
- ubuntu中由于将用户权限修改为root导致只能使用guest进入图形化界面的解决方法
- 【手把手教你全文检索】Apache Lucene初探
- Java关键字final、static使用总结
- 自定义菜单
- lightoj1292Laser Shot【共线判断求共线的最多点数】
- Android Custom View NullPointerException
- 1111
- c++封装socket
- UE4的Collision与Physics
- 英语,你好! 10月周报(一)
- ScrollView嵌套ListView问题
- 5555
- codeforces 584C Marina and Vasya
- 买一送一的最小代价问题