简体   繁体   English

通过ID从以编程方式添加的EditText中获取文本

[英]Getting text from programmatically added EditText by id

I have a series of programmatically added EditTexts in a Linearlayout with id's that I assigned using the 我在Linearlayout中以编程方式添加了一系列EditText,它们的ID是我使用

int id = 100;
EditText field =  new EditText(getActivity());
field.setId(id);

I need to get the text from the EditText's and I keep getting nullPointerException when I try to get the text from it. 我需要从EditText中获取文本,当我尝试从中获取文本时,我不断得到nullPointerException。

EditText temp = (EditText)view.findViewById(id);
ans = ans+Double.parseDouble(temp.getText().toString());

Also the view is in a fragment if that makes any difference. 如果有任何区别,视图也将处于碎片中。

Thanks for any answers. 感谢您的回答。

just make the field as a member variable instead a local variable. 只需将field作为成员变量而不是局部变量即可。 Or make sure that view is the view where you have added your field 或确保该view是您添加field的视图

将id变量声明为private并继续使用id ++添加edittext,然后尝试获取id即可获取EditText的id,直到获得空指针异常为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM