Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Cant create handler inside thread that has not called looper.prepare, eventlisteners

I have a class which extends Thread. This class will read some bytes over bluetooth, and once its finished, or the last byte is read, it will call an event listener in order to update all the listening classes.

Like this in the bluetooth class:

fileCompleteInitiator.fileCompleted(fileName);

The implemented method creates an AsyncTask in order to process this file:

public class Home extends Activity {

   //other methods like onCreate..

    @Override
    public void fileComplete(String fileName) {

     if(fileName.endsWith(".zip")) {    
        CaseGenerator generator = new CaseGenerator(Home.this, fileName, lastCases, nist); 
        generator.execute("");
      }
    }
}

Inside this asynctask, I want to show a progressdialog, but I get the not-so-original exception:

Cant create handler inside thread that has not called looper.prepare

The code for it is the usual thing, create the progressdialog in onPreExecute() and dismiss it in the onPostExecute(). Some code

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.setTitle("Please Wait..");
        pd.setMessage("Generating file");
        pd.show();
    }
     .....................

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        if(pd != null)
            pd.dismiss();
     }

What am I missing?

Upvotes: 0

Views: 82

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

You need to be creating and executing your AsyncTask from the main application thread. That's a requirement of AsyncTask anyway, last I checked, and it will address your onPreExecute() problems.

Upvotes: 1

Related Questions