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();
 }