您的位置:首页 > 移动开发 > Android开发

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

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: