获取编辑文本错误空指针为什么值? [英] Getting values of edit text error NULLPOINTER why?

查看:221
本文介绍了获取编辑文本错误空指针为什么值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从编辑字段值文本显示对一个对话框,并保存在一个变量。

 最后对话的对话=新的对话框(背景);
dialog.setContentView(R.layout.dialg);
dialog.setTitle(标题...);
dialog.show();
按钮dialogBu​​ttonCancel =(按钮)dialog.findViewById(R.id.cancel);
//如果点击按钮,关闭对话框定制
dialogBu​​ttonCancel.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        dialog.dismiss();}});
按钮dialogBu​​ttonOK =(按钮)dialog.findViewById(R.id.OK);
// ************ ***************************
dialogBu​​ttonOK.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        的EditText edt1 =(EditText上)findViewById(R.id.EditTextNom);
        。NOM = edt1.getText()的toString();
        的EditText edt2 =(EditText上)findViewById(R.id.editTextDescription);
        描述= edt2.getText()的toString();
        dialog.dismiss();
    }});


解决方案

尝试,而不是

 的EditText edt1 =(EditText上)dialog.findViewById(R.id.EditTextNom);

您需要在膨胀的对话框布局看。现在,它正在寻找在被夸大的活动之一,很明显,那些查看■不要存在于该布局

I'm trying to get text from values of edit field showed on a dialog and save it in a variable.

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialg);
dialog.setTitle("Title...");
dialog.show();
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
// if button is clicked, close the custom dialog
dialogButtonCancel.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();}});
Button dialogButtonOK = (Button) dialog.findViewById(R.id.OK);
//***************************************************************************
dialogButtonOK.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText edt1=(EditText)findViewById(R.id.EditTextNom);
        nom = edt1.getText().toString();
        EditText edt2=(EditText)findViewById(R.id.editTextDescription);
        description = edt2.getText().toString();
        dialog.dismiss();
    }});

解决方案

Try instead

 EditText edt1=(EditText)dialog.findViewById(R.id.EditTextNom);

you need to look in the layout that is inflated for the Dialog. Right now it is looking in the one that was inflated for the Activity and, obviously, those Views don't exist in that layout.

这篇关于获取编辑文本错误空指针为什么值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆