I have two classes, MainActivity
and DoHardWork
. DoHardWork extends AsyncTask
, and in the class I need to access a context in order to get the contentResolver
and do a query.
No problem, right? Let's just pass it as a parameter to DoHardWork
:
Context currCont = this;
new DoHardWork(currCont).execute();
Then in the constructor I grab the context and store it in a global variable called ccc
.
But as soon as I try to access the context it crashes, with no errors that make sense.
try {
Cursor cursor = ccc.getContentResolver().query(
Uri.parse("content://sms/inbox"), null, null, null, null);
}
catch (Exception e) {
Log.e("apptag", e.getMessage());
e.printStackTrace();
}
All I get is:
05-18 18:54:06.725: E/apptag(29063): Crashed
05-18 18:54:06.725: E/apptag(29063): java.lang.NullPointerException
05-18 18:54:06.725: E/apptag(29063): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
05-18 18:54:06.725: E/apptag(29063): at se.jbhalmstad.ndroid.DoHardWork.getTextMessages(DoHardWork.java:214)
05-18 18:54:06.725: E/apptag(29063): at se.jbhalmstad.ndroid.DoHardWork.returnResults(DoHardWork.java:114)
05-18 18:54:06.725: E/apptag(29063): at se.jbhalmstad.ndroid.DoHardWork.doInBackground(DoHardWork.java:55)
05-18 18:54:06.725: E/apptag(29063): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-18 18:54:06.725: E/apptag(29063): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-18 18:54:06.725: E/apptag(29063): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-18 18:54:06.725: E/apptag(29063): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-18 18:54:06.725: E/apptag(29063): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-18 18:54:06.725: E/apptag(29063): at java.lang.Thread.run(Thread.java:856)
Why can't I access the context?