Getting values of edit text error NULLPOINTER why? [closed]

穿精又带淫゛_ 提交于 2019-12-20 07:55:57

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/22724710/getting-values-of-edit-text-error-nullpointer-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!