-1

My app's behavior runs on a container activity with different fragments on it. Now inside those fragments I perform async tasks in order to retrieve data from web API. I'm experiencing a problem where when the async task is not yet done, and I tried to switch fragments, it causes an error. I kinda get the logic of the error because after the task is done it will go to onPostExecute() to perform other UI related functions after the task and the fragment where it was executed is now gone and now has a new fragment. Is there a way to solve this? I already included setRetainInstance(true) for the fragment as per research but still the error persists.

Here's I switch fragments:

Fragment fragment = new Fragment();
fragment.setRetainInstance(true); 
fragment = new MyFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.main_container, fragment)
                       .commit();

Here's a sample of my asyncTask

private class SomeTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... id) {
        // TODO Auto-generated method stub
        //do some logic here on getting data from web api

        return null;
    }

    protected void onPostExecute(Void x) {
        //do some UI functions here
    }

 }

I usually use void on my parameters because I often use class/global variables so I could access them anywhere within my class.

UPDATE:

Logcat error:

09-01 14:50:33.764: E/AndroidRuntime(617): FATAL EXCEPTION: AsyncTask #2
09-01 14:50:33.764: E/AndroidRuntime(617): java.lang.RuntimeException: An error occured while executing doInBackground()
09-01 14:50:33.764: E/AndroidRuntime(617):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-01 14:50:33.764: E/AndroidRuntime(617):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.lang.Thread.run(Thread.java:856)
09-01 14:50:33.764: E/AndroidRuntime(617): Caused by: java.lang.NullPointerException
09-01 14:50:33.764: E/AndroidRuntime(617):  at com.nurserversity.com.Helpers.ApiHelper.<init>(ApiHelper.java:49)
09-01 14:50:33.764: E/AndroidRuntime(617):  at com.nurserversity.com.DashboardHome$GetProductBundlesTask.doInBackground(DashboardHome.java:115)
09-01 14:50:33.764: E/AndroidRuntime(617):  at com.nurserversity.com.DashboardHome$GetProductBundlesTask.doInBackground(DashboardHome.java:1)
09-01 14:50:33.764: E/AndroidRuntime(617):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-01 14:50:33.764: E/AndroidRuntime(617):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-01 14:50:33.764: E/AndroidRuntime(617):  ... 5 more
2
  • Do Post your logcat error along with some sufficient code. Your doesn't show necessary part
    – GrIsHu
    Sep 1, 2014 at 7:05
  • Show the code of line no 115. of your DashboardHome$GetProductBundlesTask.doInBackground(DashboardHome.java:115)
    – GrIsHu
    Sep 1, 2014 at 7:10

2 Answers 2

0

I can think of two options:

  1. Abort the AsyncTask when you switch fragments.

  2. In onPostExecute(), store the data in a way that can later be retrieved when the fragment is displayed again. For example, you can throw the data into a SQLite database or SharedPreferences.

1
  • Hmm, this could be a nice alternative. How can I abort running asyncTasks? I'm thinking of aborting every running async task when switching fragments. Sep 1, 2014 at 7:10
0

I had the same error. I simply stopped async task when we move out of a fragment.

@Override
    public void onStop() {
        super.onStop();
        if(postToServerAsyncTask!=null && postToServerAsyncTask.getStatus()==Status.RUNNING){
            postToServerAsyncTask.cancel(true);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.