when im running an method from an asynctask, my application crashes. But i am not sure why, the error log isn’t really elaborate as well, which makes it harder to find the problem.
It has something to do with the context, can anyone find what i am doing wrong?
public class UpdateHelpers extends Activity{
public String UPDATE_TIMESTAMP;
//opgeslagen timestamp verkrijgen
public int getLastUpdateTimeStamp(){
System.out.println(" getLastUpdateTimestamp Methode" );
SharedPreferences savedTimeStamp = getSharedPreferences(UPDATE_TIMESTAMP, MODE_PRIVATE);
Integer startvalue = 1343192400; // timestamp last file from assets: (25-07-2012)
Integer timestamp = savedTimeStamp.getInt("timestamp", startvalue); //standaard value: datum laatste json uit assets (25-07-2012)
System.out.println("return value: "+timestamp);
return timestamp;
}
Class is bigger, more helping methods, but this already doesnt work)
In the Async task (do in background)
//check for updates
Integer ts = uh.getLastUpdateTimeStamp();
System.out.println("value of timestamp= "+ts);
where uh is declared in the onCreate of the class with the async task:
public UpdateHelpers uh;
uh = new UpdateHelpers();
What i need is an saved timestamp, and if it doesnt exist the timestamp from when i downloaded an file (on 25 juli). Then i can check if there is an update with another method, but my code crashes earlier.
I put in some system.out.printlns, so you can see where it goes wrong
Here is my crash log:
08-26 16:14:54.932: I/System.out(12571): getLastUpdateTimestamp Methode
08-26 16:14:54.932: W/dalvikvm(12571): threadid=8: thread exiting with uncaught exception (group=0x400259f8)
08-26 16:14:54.942: E/AndroidRuntime(12571): FATAL EXCEPTION: AsyncTask #1
08-26 16:14:54.942: E/AndroidRuntime(12571): java.lang.RuntimeException: An error occured while executing doInBackground()
08-26 16:14:54.942: E/AndroidRuntime(12571): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.lang.Thread.run(Thread.java:1102)
08-26 16:14:54.942: E/AndroidRuntime(12571): Caused by: java.lang.NullPointerException
08-26 16:14:54.942: E/AndroidRuntime(12571): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
08-26 16:14:54.942: E/AndroidRuntime(12571): at com.hera.ontdekdelft.UpdateHelpers.getLastUpdateTimeStamp(UpdateHelpers.java:24)
08-26 16:14:54.942: E/AndroidRuntime(12571): at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:611)
08-26 16:14:54.942: E/AndroidRuntime(12571): at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:1)
08-26 16:14:54.942: E/AndroidRuntime(12571): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-26 16:14:54.942: E/AndroidRuntime(12571): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-26 16:14:54.942: E/AndroidRuntime(12571): ... 4 more
Anyone an idea?
I didn’t put in my whole code because its a lot.
Thank you!
You cannot initialize a activity class like below. It needs to be initialized by the Android for you
uh should be your Activity. Do something like this