I am pretty new to async tasks, but the few I have created for my app entail accessing class level variables and I have had a few null pointer exceptions sometimes in the doInBackground() for these tasks on real-world devices.
I believe this is because these variables should not really ever be accessed from within the async task, but instead passed in through the constructor or params etc. For example if I transition to activity B and an async task is still running from activity A, all of A's variables should become null shouldn't they?
And if accessing these variables should never be used from within the context of an async task, shouldn't the IDE (Android Studio in my case) warn me this is a dangerous action?
Edit: Wow so much help so quickly! Hopefully this will help others in the future too. Here is some code which is roughly what I was thinking about:
public class MyActivity extends ActionBarActivity {
private Context mContext;
private User mUser;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
mProgressDialog = new ProgressDialog(mContext);
mUser = new User();
new MyAsyncTask.execute();
}
private class MyAsyncTask extends AsyncTask<Params, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(true);
mProgressDialog.setMessage("My message");
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
@Override
protected Void doInBackground(Params... params) {
Toast.makeText(mContext, "In doInBackground()", Toast.LENGTH_LONG).show();
mUser.someMethodThatTakesTime();
return null;
}
@Override
protected void onPostExecute(Void params) {
mProgressDialog.dismiss();
//Possibly launch another activity
}
}
}