Skip to content Skip to sidebar Skip to footer

Can't Create Handler Inside Thread That Has Not Called Looper.prepare() With Asynctask

I am trying to use the AsyncTask class on android but I have a problem. Here is my code: public void CheckIfUserIsLogedIn () { int userPresence = localDatabase.CheckUserPresen

Solution 1:

Seems you are doing some stuff to Update your UI from a Non-UI thread. So, you should put your stuff to update the UI inside runOnUiThread() so that it executes the stuff on your UI hread itself.

UPDATE:

Just try to put your stuff for getting CheckCountryPresence(); inside onPreExecute(). So your edited class should be like,

classcheckCountryAsyncextendsAsyncTask<String, String, String> {

    int countryPresence = 0;
    @OverrideprotectedvoidonPreExecute() {
       countryPresence = localDatabase.CheckCountryPresence();
    }
    @OverrideprotectedStringdoInBackground(String... aurl) {

        Activity_name.this.runOnUiThread(newRunnable() {

        @Overridepublicvoidrun() {
            if (countryPresence == 0) 
            {
              CountryTable country = newCountryTable() ;
              country.EnterCountry();
            }
        }
    });
        returnnull;
    }
}

Solution 2:

Has your CountryTable class any interaction with the UI activity/class? Even fetching new rows for your adapter object? If yes, you cannot update your activity from outside the activity itself.

Fortunately you can overcome this pretty easily once you figure out the solution ;)

step 1:

Define an interface, like CheckCountryCallback with a single method called onCheckCountryFinished(String taskResult);

Step 2: let the activity which call your asynctask implement this interface and its method.

Step 3: define a constructor for your asyncTask with a single parameter of type CheckCountryCallback

Step 4: override onPostExecute of your AT, and simply write mCaller.onCheckCountryFinished(result).

This is not enough sadly because you'd still be updating the ui from another thread but we're getting close!

In your implemented onCheckCountryFinished you should redirect the call to a handler you've to define in your activity. Send a message to that handler and you can update the UI (or the adapter or whatnot) from the handle message method of the handler.

Handler example:

privatestaticfinalintUPDATE=2;
privateHandlerhandler=newHandler() {

    publicvoidhandleMessage(Message msg) {
        if (msg.what == UPDATE) {
               //do something with m.obj
               myAdapter.notifyDataSetChanged();
        }
    }
};

callback example

publicvoidonCheckCountryFinished(String result){
    Message m = Message.obtain();
    m.what = UPDATE;
    m.obj = result;
    handler.sendMessage(m);
}

Solution 3:

You are not passing your AsyncTask anything so make the parameters of AsyncTask all Void like this AsyncTask<Void, Void, Void> then change doInBackground to this protected Void doInBackground(Void... voids). Also, get rid of the onPreExecute method since you are not even using it.

Also, it is unclear with you code provided, but CheckIfUserIsLogedIn needs to be called from the UI thread. AsyncTask is really only meant to be run off of the UI thread.

Solution 4:

one of the problem that i faced, simpleCursorAdapter also returns same error of looper when called from thread. So to remove that error, inside thread

ActivityName.this.runOnUiThread(new Runnable() {
 publicvoidrun() {
   String[] FROM = {/*your data here*/};
int[] TO = { /*your data here*/ };
adapter = new SimpleCursorAdapter(ActivityName.this, R.layout.row, cursor, FROM, TO);   
listTimeline.setAdapter(adapter); //your own code here.
}
});

Post a Comment for "Can't Create Handler Inside Thread That Has Not Called Looper.prepare() With Asynctask"