Android中findViewById()获取控件后 报 空指针 错误
2012-12-01 15:04
876 查看
今天再做一个程序时,发现我使用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()获取控件为NULL
- Android中findViewById获取控件返回为空问题怎么解决
- Android中findViewById()h获取EditText 空指针问题
- Android基于4.4以上沉侵式状态栏和继承BaseActivity不用findViewbyid得到控件id
- Android fragment 获取id,findViewById为空
- android getActivity.findViewById获取ListView 返回NULL
- findViewById获取控件ID时返回Null
- Android--xml控件写法和findViewById的对应使用
- android:通过findViewById试图获取界面元素对象为Null,运行会提示空指针异常
- Activity中 findViewById 获取控件对象正确的代码位置
- Android中findViewById()h获取EditText 空指针问题
- Android中findViewById()h获取EditText 空指针问题
- Android:一键find控件,从此告别繁琐的findViewById
- Android Studio @Bind的用法,自动生成findViewById无需再实例化控件
- android getActivity.findViewById获取ListView 返回NULL的方法
- Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决
- 求助 android开发中 如果两个控件的id相同 会怎样?如何使用findviewbyid ()寻找到?
- Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决
- 【android中findViewById返回控件指针为NULL】
- android 自动初始化控件 不用注解不用findViewByid