0

I am using animation in my app, Whenever activity starts i set the contentview to that animation class but whenever user go back it crashes its happening only where i have used the animation..Can you tell me how can i save my app from this crash

    import android.app.Activity;

import android.media.MediaPlayer;
import android.os.Bundle;

public class Ultimate_bomb extends Activity {
    FireworkLayout my;
    MediaPlayer sound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        my = new Animation(this);
        setContentView(my);
        sound = MediaPlayer.create(Ultimate_bomb.this, R.raw.ultimate_bomb);
        sound.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        sound.release();
        finish();
    }
}
  • Animation can not be android's class. You can't set an animation as a contentView of activity. – Devrim Oct 27 '13 at 16:13
  • Please read the documentation and follow some tutorials. You cannot setContentView to an animation. – Simon Oct 27 '13 at 16:13
  • @aegean Animation is not my class i have changed the name so that you can understand that is the class for animation –  Oct 27 '13 at 16:20
  • @Simon sir i have used setContentView to animation its working fine but thing when user click back it crashes..sir i am beginner how can i implement animation so that user can see it on screen with music playing in background..and i am sorry i am running out of mind so cant read documentation i am very much tensed because i have already launched my app and user are facing this issue..please tell the solution sir –  Oct 27 '13 at 16:22
  • @Pushendra if you are looking for a solution you must share your Animation class. I'm definitely sure it is not an android class. BTW please try your code with removing finish() at onPause. – Devrim Oct 27 '13 at 16:52

1 Answers1

0

I guess you are playing an animation or starting an asyncTask at your Animation class. When you press to back button your activity is destroyed but your animation or task at your Animation class tries to do sth with context or views(probably an imageView) that attached to the context. To solve your problem you must stop your animation or set a flag at Animation class, at onPause method of your Activity.

Edit: I looked into FireworkLayout class you used(by the link you shared). As I see there is a class named as FireworkLayout and inside this class we have a GameThread. This thread does draw if AnimateState is asRunning. You must set it to asPause when your activity goes to on pause state.

Ok now, I am enlightened, I guess you too:) Add this medhod to FireworkLayout class and call onActivityPaused when your Activity goes onPause state.

public void onActivityPaused() {
    if(thread != null) {
        thread.pause();
    }
}

BTW you may need a resume method. So add this method to FireworkLayout class and call it when your activity is on onResume state.

public void onActivityResumed() {
    if(thread != null) {
        thread.unpause();
    }
}

Sample call after edits;

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    my.onActivityPaused();
    ...

}

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • hey sir how can i stop animation at pause can you show in code or set a flag at animation class and wait here is the code of my animation class –  Oct 27 '13 at 17:26
  • sir i am unable to show code in comment can you tell me the other way there i can show my code –  Oct 27 '13 at 17:26
  • http://stackoverflow.com/questions/5408204/how-to-create-fireworks-particle-graphics-effect-on-android sir see the codes on this post –  Oct 27 '13 at 17:59
  • I looked into the FireworkLayout that you used and edite my answer. Hope that helps. – Devrim Oct 27 '13 at 18:07
  • sir wait aq min i will just add it –  Oct 27 '13 at 18:09
  • sir i am just a beginner can you show me in code where should i add this in firworkslayout class –  Oct 27 '13 at 18:11
  • You can add my onActivityPaused method after FireworkLayout 's surfaceDestroyed method. – Devrim Oct 27 '13 at 18:12
  • aegean sir thank you will be less fir your help sir really i want to say thank you by my heart sir can i add you on facebook or on some some social networking site ? –  Oct 27 '13 at 18:27
  • :) I'm glad that your problem is solved. Here in stack all try to solve problems of others or try to find an answer of a problem. This is it. Maybe next time you are going to answer a question of mine;) – Devrim Oct 27 '13 at 18:34
  • thankyou sir i am new on stack also hahah but really thx frm my heart btw do u want to c my app ? –  Oct 27 '13 at 18:36