Android Error —— 自定义控件FindViewById() 返回 Null
来源:互联网 发布:java 代码 编辑:程序博客网 时间:2024/05/17 04:35
问题:使用findviewById() 返回空,直到初始化控件的时候才报空指针异常。
今天写android自定义控件的时候突然发现了一个问题,也不报错,实在蛋疼。后面发现是因为自定义控件构造函数使用不对,
自定义构造函数有三个构造函数。
Class(Context); 普通构造函数,无法加载属性集(无法加载XML文件中定义的控件属性,导致无法从XML文件中
始 化)。
Class(Context,Attribute); 带属性集的构造函数,可以加载属性集(同时也可以加载XML文件中定义的属性)。
Class(Context,Attribute,int); 带属性集和默认风格的构造函数。
- 附上XML布局与代码:
1、XML布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg2" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.todayfocus.view.BannerFlipper android:id="@+id/banner_fliper" android:layout_width="match_parent" android:layout_height="150dp" > </com.todayfocus.view.BannerFlipper> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="5dp" android:background="@android:color/darker_gray" android:text=" " /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" > <com.todayfocus.view.SubGridLayout android:id="@+id/grid_subscription" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:columnCount="3" > </com.todayfocus.view.SubGridLayout> </RelativeLayout> </LinearLayout> </ScrollView></LinearLayout>
2、 代码:
public class BannerFlipper extends ViewFlipper implements DataSetObserver{private Context mContext; private MyBaseAdapter mAdapter; private AttributeSet mAttrs; private int mNum = 0; public BannerFlipper(Context context){ //问题就处在这里,该构造函数没有属性参数,无法加载XML控件的参数,因此无法初始化。 super(context); mContext = context; }@Overridepublic void onChanged() { mNum = mAdapter.getCount(); update();} public void setAdapter(MyBaseAdapter adapter){ mAdapter = adapter; mAdapter.registerObserver(this); }private void update(){this.removeAllViews();for (int i = 0; i < mNum; i++) {RelativeLayout item = (RelativeLayout) mAdapter.getView(i);this.addView(item);}}}
在初始化的时候:
protected void findViews() {mBannerFlipper = (BannerFlipper) mView.findViewById(R.id.banner_fliper);//该处返回空指针,之后初始化报空指针异常mGridLayout = (SubGridLayout) mView.findViewById(R.id.grid_subscription); }
后面多次调试之后,才发现是因为控件的构造函数中没有参数,因此无法加载XML文件里面的属性用来初始化。
问题就在于,自定义控件如果要在XML里面加载,就需要构造函数中有AttributeSet参数用来加载XML属性。
0 0
- Android Error —— 自定义控件FindViewById() 返回 Null
- 7. findViewById 返回为null (自定义控件)
- android findViewById 返回 null
- android开发之自定义控件时findViewById方法返回null的解决方法
- Anroid studio遇到的问题7--- findViewById 返回为null (自定义控件)
- 【android中findViewById返回控件指针为NULL】
- findViewById获取控件ID时返回Null
- Android开发:findViewById返回null的解决办法
- Android开发:findViewById返回null的解决办法
- Android开发:findViewById返回null的解决办法
- [Android] Fragment findViewById()返回null完全解析
- Android通过findViewById()获取控件为NULL
- findViewById()返回 null
- findViewById返回null
- Andorid findViewById()返回Null
- findViewById()返回null
- findViewById返回Null 错误
- findViewById 返回null
- 第一行代码-3.6 单位和尺寸
- 挑战极限 Oracle数据库一秒导百万数据
- noip2003加分二叉树 2008.10.30
- Python装饰器学习(九步入门)
- "Edit Document" requires a Microsoft SharePoint Foundation-compatible Application错误的解决方案
- Android Error —— 自定义控件FindViewById() 返回 Null
- noip2004合唱队形2008.11.4
- hihoCoder - 1093 - 最短路径·三:SPFA算法
- 继承的好处
- ACM竞赛黑科技
- noip2002-麦森数 2008.11.4
- 解释器模式——化繁为简
- Quartz2D绘图
- 设置Eclipse console显示全部日志