简体   繁体   中英

NullPointerException after resuming the application

Ok, this annoying problem is probably quite familiar, but I don't know what its being called and how to solve it. When I open my app and go to the menu and after a meanwhile, when resuming the app, I loose all my data. It seems that android likes to clean data in order to keep the OS as fast and stable as possible. Which method is recommended in saving data in the internal memory and retrieving it back when any kind of variable is cleaned/null before resuming the app? I tried setSharedPreferences to parse an ArrayList to an Object and parse the Object as a String to save the data and retrieve it, but I get cannot parse Object to String exception. There has to be a better alternative.

Any help will be appreciated.

Edit:

This is how I retrieve and store data:

JSONObject data = (JSONObject) new JSONTokener(result).nextValue();

Helper.RAW_PEOPLE_INFO = data.getJSONArray("people");
Helper.PEOPLE_CONTAINER = new ArrayList<PeopleInfoStorage>();
for( int i = 0; i < Helper.RAW_PEOPLE_INFO.length(); i++ ){
    Helper.PEOPLE_CONTAINER.add( new PeopleInfoStorage(Helper.RAW_PEOPLE_INFO.getJSONObject(i)) );
}

I use the PEOPLE_CONTAINER ArrayList to use it later for when I need it. The PEOPLE_CONTAINER ArrayList gets probably cleaned before I resume my application, so can someone help me giving an example on how to store this ArrayList in the internal memory so I can retrieve the data from the internal memory and put it back to the PEOPLE_CONTAINER ArrayList for when it's null again.

It needs to be something like this:

@Override
protected void onPause() {  
    if( Helper.PEOPLE_CONTAINER != null ){
        //save the Helper.PEOPLE_CONTAINER ArrayList to the internal memory
    }
    super.onPause();
}

@Override
protected void onResume() {
    if( Helper.PEOPLE_CONTAINER == null ){
        //retrieve the data and store it back to Helper.PEOPLE_CONTAINER ArrayList
    }
    super.onResume();
}

Following is a brief explanation of which storage option to use at what time :

Shared Preferences : These are basically good for storing an applications preferences and other small bits of data.SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. So it becomes easy to read the data. But for large data sets it might become a bit difficult because you have to define a unique key for every key in the dataset. Ex: Store session key for user login in SharedPreferences.

Internal Storage : This is good for storing application data which the user doesnot need access to. Possibly good for storing logs or anything that the application will create/update/delete.

SQLite : Large amounts of same structured data should be stored in a SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a sub set of the data which matches certain criteria using a query language like SQL. Searching becomes easy here. While SQLite might not scale nearly as big as the dedicated databases, it is very quick and convenient for smaller applications, like Android apps. Ex: Storing your listview items OR storing recipes for a recipe app.

Hope this helps!!!

There are many methods to persist data in your application; I'm not going to go into great detail here, but you should check out these resources:

http://developer.android.com/guide/topics/data/data-storage.html

If you have an array list, it sounds like it might be worth SQLite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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