Android 自定义Dialog时出现成员变量为null的问题
来源:互联网 发布:docker golang 部署 编辑:程序博客网 时间:2024/06/05 09:25
因为项目需要,重写了Android的Dialog,代码如下:
public abstract class BaseExpandDialog extends Dialog {protected Context context;/** * 扩展对话框监听器 */protected BaseExpandDialogClickListener baseDialogClickListener;public BaseExpandDialog(Context context,int contentResId) {// TODO Auto-generated constructor stubsuper(context, R.style.Theme_Mike_BaseDialog);this.context = context;setContentView(contentResId);initialResource();initView();}
其中initialResource()是,设置对话框的宽高,initView()是一个虚函数,申明为abstract需要在子类中实现。然后自定义一个ItemsDialog继承与BaseExpandDialog
其构造函数代码:
protected TextView titleTv = null; public ItemsDialog(Context context) {super(context, R.layout.items_dialog);// TODO Auto-generated constructor stub}
在之类的initView中实现了控件的初始化:
titleTv = (TextView) findViewById(R.id.title_text);然后使用自定义的ItemsDialog:
ItemsDialog dialog = new ItemsDialog(context);dialog.setTitle(list.get(position).getName(),context.getResources().getColor(R.color.text_highlight_color));
ItemsDialog的setTitle就是调用的titleTv.setText的方法
关键问题出现了,titleTv怎么为null,已经initView中已经赋值了,进行调试的时候,也确实在InitView中赋值了
最后发现一个问题initView实在父类的构造函数中进行调用的,而Items的非static的成员变量赋值是,发生在自己的构造函数中。在ItemsDialog的构造函数中,会先执行super()或者this() (如果有的话)->然后在执行自身的非static成员变量的赋值操作->最后才是自己写在构造函数里面的逻辑操作(// TODO Auto-generated constructor stub下的语句),所以现在知道了为什么titleTv为null了,因为先执行了super()父类的构造函数,调用initView对titleTv进行了初始化,然后再调用的自身成员变量的赋值操作titleTv = null;我们现在只需要将protected TextView titleTv = null; 改成protected TextView titleTv;即可
参考:http://www.zhihu.com/question/20616186 0 0
- Android 自定义Dialog时出现成员变量为null的问题
- Android开发时遇到的一个Java基础问题--虚方法中成员变量为null
- Android解决自定义Dialog出现黑(白)框的问题
- 自定义dialog 出现的问题总结
- Android自定义Dialog对话框样式出现的边框无法填满问题
- android 自定义dialog 自定义dialog 宽度问题
- Android中使用自定义Dialog时match_parent无效的问题
- Android自定义Dialog的一些问题
- 关于android 的org.json类JsonObject解析值为null的出现的问题
- 在Dialog类或自定义类中使用View类成员变量的方法
- 静态变量没有重置为 null 的问题
- android Dialog的自定义
- 自定义Android的Dialog
- Android 自定义的Dialog
- android 自定义的dialog
- jdbc连接数据库出现的问题,Statement一直为null
- android回调是怎么实现的? --以自定义Dialog为例
- Java中String类型变量初值为null时的连接问题
- UF超滤膜:超滤膜组件技术优势
- 关于“团队建设”的反思
- HDU 1247 Hat's words(Trie)
- FilterDispatcher与StrutsPrepareAndExecuteFilter
- JQuery中$.ajax()方法参数详解
- Android 自定义Dialog时出现成员变量为null的问题
- ios中KeyChain用途
- Java读取文件方法大全
- Divide Two Integers
- POM.xml 3
- 游戏开发常用的排序算法
- Struts过滤器过滤某些字符
- UML学习(1)
- Python之美[从菜鸟到高手]--NotImplemented小析