0

I need to do play several animation asynchronously, so I've decided to implement AnimationListener with AsyncTask. Unfortunately it throws error RuntimeException: An error acuured while executing doInBackground() at the point I'm starting an animation. Is it possible to get it working with AsyncTask?

Code: public class Game extends Activity {

private static final String TAG = Game.class.getSimpleName();

private Animation animation1;
private Animation animation2;
    ...
    ...
    ...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    // animations for turning cards
    this.animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
    this.animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
        ...
        ...
    AsyncTurn turn1 = new AsyncTurn(Game.this, position);
    turn1.execute();
}

AsyncTask:

public class AsyncTurn extends AsyncTask<Void,Void,Void> implements AnimationListener{

    private Context context;
    private int actPosition;

    public AsyncTurn(Context context, int position) {
        this.context = context;
        this.actPosition = position;
        animation1.setAnimationListener(this);
        animation2.setAnimationListener(this);
    }

    @Override
    protected Void doInBackground(Void... params) {
            ((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
            ((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation1);
                // ERROR AT NEXT LINE
            ((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation1);
    }
Jakub Turcovsky
  • 2,096
  • 4
  • 30
  • 41

1 Answers1

0

Not sure why u need AsyncTask anyway but u can use the AsyncTask function

void onProgressUpdate(Progress... values)

in the doInBackground() call the function publishProgrss()

For Ref: AsyncTask

or use runOnUIThread Example : RunOnUiThread Example

Community
  • 1
  • 1
  • I need to separate variables for every animation and I don't see any other option. – Jakub Turcovsky May 05 '14 at 22:29
  • instand of using AsyncTask send the varibales here for example AsyncTask , create a Holder Class for all the variables u need and send them to the AsyncTask –  May 05 '14 at 22:30
  • I think @dannyroa is right, the exception is thrown because I'm trying to do UI stuff in background. I need to find some other way how to run those animations separately. But which way...? – Jakub Turcovsky May 05 '14 at 23:14