• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Handler HandleMessage not fired from AsyncTask

Hi,

I have an AsyncTask that is doing some processing but when it is finished I cannot get it to fire the HandleEvent method af a Handler object (I hope that terminology is correct).

Here is my code:

public class main extends Activity {
/** Called when the activity is first created. */
public Handler mHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// code here to start the background task - all working fine

mHandler = new Handler() {
public void HandleMessage(Message msg) {
Log.v("Handler", "in HandleMessage");
}
};
}
}

final class SendItemToServer extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground (Void ... arg0) {
// some code here to connect to my server - all this is working, data is saved on the server
mHandler.sendEmptyMessage(0);
return 0;
}

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

The error is on the line mHandler.sendEmptyMessage(0); in the AsyncTask - the error is "mHandler cannot be resolved".

Thanks for any help.

Simon
 
For anyone that is interested, this was a scope issue.

I was not aware in Java that you could define a class from within another class.

To resolve the problem, I moved the class SendItemToServer within the class main (and made the class SendItemToServer public class instead of final class).

Simon
 
Back
Top Bottom