Android中findViewById()获取控件后 报 空指针 错误
来源:互联网 发布:中学生当街打母 知乎 编辑:程序博客网 时间:2024/04/28 14:27
今天再做一个程序时,发现我使用findViewById(R.id.edit)获取EditText时总是报空指针错误,我想不可能啊!!
最后从findViewById()下手,才发现原来此方法中的R.id.edit是从当前Activity或者Dialog的主布局文件xml中获取。
比如:我的程序:
ListActivity类中:
。。。。。。。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
}
。。。。。
/**
* 显示弹出的输入窗口
* */
public void showInputDialog(FileBean fileBean) {
LayoutInflater layoutInflater = getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.input_dialog,
(ViewGroup) findViewById(R.id.input_dialog));
EditText editText = (EditText)layout.findViewById(R.id.input_content);// 获取输入文本框 如果改成EditText editText = (EditText)this.findViewById(R.id.input_content);// 空指针错误
new AlertDialog.Builder(this)
.setTitle("重命名文件" + new File(fileBean.getPath()).getName())
.setView(layout)
.setPositiveButton("确定", new MyDialogListener(editText))
.setNegativeButton("取消", new MyDialogListener(editText)).show();
}
最后从findViewById()下手,才发现原来此方法中的R.id.edit是从当前Activity或者Dialog的主布局文件xml中获取。
比如:我的程序:
ListActivity类中:
。。。。。。。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
}
。。。。。
/**
* 显示弹出的输入窗口
* */
public void showInputDialog(FileBean fileBean) {
LayoutInflater layoutInflater = getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.input_dialog,
(ViewGroup) findViewById(R.id.input_dialog));
EditText editText = (EditText)layout.findViewById(R.id.input_content);// 获取输入文本框 如果改成EditText editText = (EditText)this.findViewById(R.id.input_content);// 空指针错误
new AlertDialog.Builder(this)
.setTitle("重命名文件" + new File(fileBean.getPath()).getName())
.setView(layout)
.setPositiveButton("确定", new MyDialogListener(editText))
.setNegativeButton("取消", new MyDialogListener(editText)).show();
}
- Android中findViewById()获取控件后 报 空指针 错误
- Android中findViewById()h获取EditText 空指针问题
- Android中findViewById()获取EditText 空指针问题
- Android中findViewById()h获取EditText 空指针问题
- Android中findViewById获取控件返回为空
- 在Fragment中加载布局后使用findViewById出现空指针错误
- findViewById报空指针异常
- 自定义控件findViewById得到空指针,错误的排除
- 自定义控件findViewById得到空指针,错误的排除
- android findviewByid()关于空指针
- 自定义控件获取报空指针异常
- Android新版SDK中findViewById返回空指针问题
- 【android中findViewById返回控件指针为NULL】
- Android下findViewById遇到空指针异常
- android自定义控件findViewById为空
- Android fragment 获取id,findViewById为空
- 打开myEclipse后,tomcat报空指针错误
- android:通过findViewById试图获取界面元素对象为Null,运行会提示空指针异常
- struct结构的大小
- 加速matlab运行的三重境界
- C++面试题解析
- [hadoop源码阅读][4]-org.apache.hadoop.io.compress系列3-使用压缩
- PHP学习之路六(php与正则表达式)
- Android中findViewById()获取控件后 报 空指针 错误
- 第二章(Unicode)之---格式化的消息框
- Visual Basic 2008 中 数据库连接模块
- 牛腩新闻发布系统总结之系统总结篇
- android 电话本 多音字处理
- i2c - gpio
- 各大软件公司经典算法面试题
- 线程池
- 两个头文件相互包含时出现的问题