EditText 上的 android 随机 NullPointerException
对于 Settings 类和关联的 XML 页面,大约 50% 的访问时间我会收到 NullPointerExceptions。典型的会话在启动后不久就会尝试加载当前设置。用户可以继续填写订单、查看过去的订单或更新/查看当前设置。
有时设置不会加载,有时也不会保存。 加载:
((EditText) parent.findViewById(R.id.txtCompanyName))
.append(companyName);
保存:
companyName = ((EditText) parent.findViewById(R.id.txtCompanyName))
.getText().toString();
这些是各自功能的第一行。我不确定他们为什么会引发此异常(主要是保存功能)。据我所知,加载函数可能会在视图完全加载之前调用,但是,保存函数只能在视图完全加载之后发生(它保存在按钮的 android:onClick 上)。
For a Settings class and associated XML page, I receive NullPointerExeceptions about 50% of the time it is accessed. A typical session attempts to Load the current settings shortly after startup. The user can the proceed to fill out an order, review past orders, or update/view current settings.
Sometimes the settings don't load and other times they won't save.
Loading:
((EditText) parent.findViewById(R.id.txtCompanyName))
.append(companyName);
Saving:
companyName = ((EditText) parent.findViewById(R.id.txtCompanyName))
.getText().toString();
These are the first lines of their respective functions. I am uncertain why they would raise this Exception (mostly the saving function). As near as I can tell, the loading function may be called before the View is fully loaded, however, the save function can only occur after the the View IS fully loaded (it saves on a android:onClick for a Button).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 SharedPreference 来保存用户设置的持久状态。
这将是一个更好更有效的方法。
如果 SharedPreference 中没有任何内容,它永远不会返回 null,除非您将其设置为这样做。
如果您需要这方面的示例,请告诉我。
What i would recommend to you is use a SharedPreference to save a persistant state of the user's settings.
This would be a better more efficient way.
And if there isnt anything in the SharedPreference it will never return null unless you set it to do so.
Let me know if you need an example of this.