0

The following code gives Fatal Exception Async Task #2 on the line v1.setEnabled(false).

It means to disable button on successful call. The v.setEnabled(false); before background task works fine. Please help :(

public void onClick(View v) {
    Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
    //this one would work
    //v.setEnabled(false);

    final View v1=v;
    mRegisterTask1 = new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        boolean success = 
            ServerUtilities.receipt (((String)v1.getTag()).substring(3),"acknowledged");

        if (success) {
            //this one causes Async Task exception
            v1.setEnabled(false);
        } 
        else {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mRegisterTask1 = null;
    }
Xavi López
  • 27,550
  • 11
  • 97
  • 161
selytch
  • 535
  • 2
  • 9
  • 24
  • And the stack trace is... – m0skit0 Feb 04 '13 at 10:13
  • You can't change widgets from `doInBackground` as that method runs on a background thread. Either use `runOnUiThread` or post a `Runnable` on `v1` with the `post` method to change the state. – user Feb 04 '13 at 10:14

3 Answers3

5

You can't change the UI state from background thread.
AsyncTask onPreExecute() & onPostExecute() methods called in main thread.
Take a look :

public void onClick(View v) {
    Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
    //this one would work
    //v.setEnabled(false);


    final View v1=v;
    mRegisterTask1 = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean success =
                    ServerUtilities.receipt (((String)v1.getTag()).substring(3),"acknowledged");


            return success;
        }

        @Override
        protected void onPostExecute(Bolean result) {

        if (result) {
                //this one causes Async Task exception
                v1.setEnabled(false);
            } else {

            }

            mRegisterTask1 = null;
        }
    }
}
EvZ
  • 11,889
  • 4
  • 38
  • 76
0

Move the ui modification to onPostExecute

boolean success = false;

protected Void doInBackground(Void... params) {
      success = ServerUtilities.receipt (((String)v1.getTag()).substring(3),"acknowledged");

        if (success) {
               //this one causes Async Task exception
                 v1.setEnabled(false);
          } else {

           }
                    return null;
             }
@Override
 protected void onPostExecute(Void result) {
      if (success) {
         //this one causes Async Task exception
            v1.setEnabled(false);
        } else {

        }
       mRegisterTask1 = null;
  }
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • That works! Now could you explain difference between Void and void? (since void did not work...) – selytch Feb 04 '13 at 10:36
0

You need to change widgets from the UI thread. The methods onProgressUpdate and OnPostExecute are executed in the UI therad. So you may consider to move your code to the latter.

Stefan
  • 4,645
  • 1
  • 19
  • 35