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).
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.
精彩评论