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

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

问题描述

我试图从对话框中显示的编辑字段的值中获取文本并将其保存在一个变量中.

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

推荐答案

试试

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

您需要查看为 Dialog 膨胀的布局.现在它正在查看为 Activity 膨胀的那个,显然,那些 View 不存在于那个 layout 中.

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.

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

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