0
public class Container {

    public process(T result){
       // Update UI
    }

    public anotherMethodThatUpdatesUI(T result){
       // Update UI
    }
    public SomeTask extends AsyncTask<T,T,T> {
        private Container container;
        public someTask(Container container){
            this.container = container; 
        }

        protected void onPostExecute(T result) {
            super.onPostExecute(result);
            container.process(result);

        }
    }
}

I first call the AsyncTask execute method which works correctly. But after that calling anotherMethodThatUpdatesUI fails with this exception:

Can't create handler inside thread that has not called Looper.prepare()

But if I remove the refrence to Container inside the AsyncTask and call process directly. I don't get the error.

public SomeTask extends AsyncTask<T,T,T> {
    private Container container;
    public someTask(Container container){
        // this.container = container; 
    }

    protected void onPostExecute(T result) {
        super.onPostExecute(result);
        process(result);
    }
}
Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
LonsomeHell
  • 573
  • 1
  • 7
  • 29

1 Answers1

0

You are calling the anotherMethod fomr inside doInBackground probably, since its another thread you need to publish a message to the original one, you can do it with publishProgress from inside doInBackground and handle at onProgressUpdated method from AsyncTask.

public SomeTask extends AsyncTask<T,T,T> {
    private Container container;
    public someTask(Container container){
        this.container = container; 
    }
    public void doInBackground(T ... args) { 
         publishProgress(step1);
         publishProgress(step2);
         publishProgress(step3);
    }
    void onProgressUpdate(T step){
         anotherMethodThatUpdatesUI(step);
    }
}
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • The async task finishes correctly and updates the UI without any problem. The problem happens after that. The weird thing the problem only arises if the AsyncTask keeps a reference to the parent object if it doesn't store a reference to the object the code works fine. – LonsomeHell Apr 18 '18 at 07:08
  • @LonsomeHell are you creating a new View inside doInBackground? – Marcos Vasconcelos Apr 18 '18 at 13:16
  • I was able to get around the problem I will edit the question tomorow. What I am looking for is not the fix but the reason why keeping an instance method inside an asyncTask creates the problem. But when the same method in the same object is accessed without keeping a reference inside. The solution stops working. – LonsomeHell Apr 18 '18 at 14:16
  • I am not creating a View instance inside doInBackground. I am accessing a backend web server to retrieve data. – LonsomeHell Apr 18 '18 at 14:17
  • It should not ever be a problem, the only problem is acessing views fomr different threads than the one that creates it. If you add enough code to reproduce the problem we can tell what it is – Marcos Vasconcelos Apr 18 '18 at 14:25