I have the following code:
public class TestSynch extends Activity {
public static ArrayList<HashMap<String,String>> list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Context ctx = this.getApplicationContext();
new ProcessFileTask().execute(ctx);
Intent i = new Intent(TestSynch.this, ListSchools.class);
i.setAction(Intent.ACTION_VIEW);
i.putExtra("arraylist", list);
startActivity(i);
}
private class ProcessFileTask extends AsyncTask<Context, Void, ArrayList<HashMap<String,String>>> {
@Override
protected ArrayList<HashMap<String,String>> doInBackground(Context... ctx) {
ArrayList<HashMap<String,String>> threadList = FileOperations.getListAsMaps(ctx[0]);
return threadList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
list = result;
return;
}
}
I can debug to the return threadList; line in doInBackground, and the ArrayList variable threadList is perfectly correct.
However, onPostExecute doesn’t seem to get called and the Activity crashes on return from doInBackground, with LogCat suggesting a nullPointerException attempting to fire the Intent (which I have previously tested and worked ok before I started messing with AsynchTask)
Any idea why this is happening? The code seems simple enough…
Thanks
I think Waqas is correct. You are performing an
AsyncTaskthat after it has executed sets apublic staticvariablelist. This list is used to pass into theIntentthat you create inonCreate. However there is no guarantee that theAsyncTaskhas actually completed at the point when you create theIntent. As such the value oflistat this point isnull.So, as Waqas suggests, you need to create the
Intentafter you have set a value forlist, i.e. withinonPostExecute.