android - Shared preference is taking wrong context -
Hello I am saving my data using shared priority . In my project 2 Activity activities are 1 I am saving data in asynctask >> doinBackground
method inside shared priority
. In Async work I am downloading many files from the server. When my first file download has started and I am now changing the activities, my activity in the background -1 and my activity in foreground -2 My file is downloading in the background My call to shared priority
is also being made at this time SharedePreference
returns an empty reference and this dual-pointer gives an exception.
If activity -1 in the foreground is not getting a null pointer exception I
02-18 06:45: 55.503: W / system. Er (1242): If any other activity comes in front of the foreground, Java.lang.NullPointerException 02-18 06: 45: 55.503: W / Systems. Er (1242): On info.helper MyPreferences.savePreferences (MyPreferences.java:17) 02-18 06:45: 55.503: W / Systems. ARR (1242): info.activity.DownloadActivity $ DownloadFileFromURL.doInBackground (DownloadActivity.java:820) 02-18 06:45: 55.503: W / Systems. Er (1242): info.activity.DownloadActivity $ DownloadFileFromURL. DoInBackground (DownloadActivity.java:1) 02-18 06:45: 55.503: W / Systems. Er (1242): Android.OS.Sync Task $ 2.Coll (Asinka TaskJava 287) 02-18 06:45: 55.503: W / Systems. Er (1242): java.util.concurrent.FutureTask $ Sync.innerRun (FutureTask.java.35) 02-18 06:45: 55.503: W / Systems. Er (1242): On Java. Util.concurrent.FutureTask.run (FutureTask.java:137) 02-18 06:45: 55.503: W / Systems. Er (1242): Android.OS.Sync Task $ Serial Exchanger $ 1.R (Async Tasks Java 30) 02-18 06:45: 55.503: W / Systems. Er (1242): java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1076) 02-18 06:45: 55.503: W / Systems. Er (1242): On Java. Util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java lessons69) 02-18 06:45: 55.503: W / Systems. Er (1242): java.lang.Thread.run (Thread.Java: 856) 02
Edit # 1
To save application data, normal Method:
Save the public static zero Reference (Reference Reference, String Key, String Value) {Share Shared Shared References Reference = Reference. Shared references (MY_ST_PREFS, Context.MODE_PRIVATE); Editor Editor = Shared References Edit (); Editor.putString (key, value); Editor.commit (); }
Save data inside AsyncTask like this:
MyPreferences.savePreferences (DownloadActivity.this.getActivity (), "downloading_storyid", downloadingStoryId); Because you are using references in asyntask, this is not pointing to the activity of asyntask
You have to pass the reference in asyntask like this:
youractivityname.this;
Comments
Post a Comment