Android 为什么我的动画在返回到包含动画的活动时会冻结?

Android 为什么我的动画在返回到包含动画的活动时会冻结?,android,animation,android-activity,imagebutton,freeze,Android,Animation,Android Activity,Imagebutton,Freeze,我对Android还是有点陌生,所以如果这是一个明显的错误,请原谅我。在本活动中,我使用ViewPager水平滚动三个布局,其中包含一个ImageButton,该按钮根据其当前状态具有动画背景。当按下按钮时,它将启动一个新的活动。但是,当我单击“上一步”按钮返回包含新活动中动画的活动时,有时动画会冻结或播放得比实际速度快。我编写了一个方法来启动我在onWindowFocusChanged()和onRestart()中使用的动画。我在安卓2.1(API 7)中工作 这是我的代码: public c

我对Android还是有点陌生,所以如果这是一个明显的错误,请原谅我。在本活动中,我使用
ViewPager
水平滚动三个布局,其中包含一个
ImageButton
,该按钮根据其当前状态具有动画背景。当按下按钮时,它将启动一个新的活动。但是,当我单击“上一步”按钮返回包含新活动中动画的活动时,有时动画会冻结或播放得比实际速度快。我编写了一个方法来启动我在
onWindowFocusChanged()
onRestart()
中使用的动画。我在安卓2.1(API 7)中工作

这是我的代码:

public class CopyOfWorld extends Activity{

    MediaPlayer muzak;
    Boolean mSwitch = false;
    ImageButton holmes;
    AnimationDrawable holmesAnimation;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.world);

        Preferencer pp = (Preferencer)getApplicationContext();

        ViewPagerAdapter adapter = new ViewPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);


        if(pp.getMuzak()){
            mSwitch = true;
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
    }

    public void clicker(View v){
     Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
        startActivityForResult(myIntent, 0);
   }

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        beginRender();

    }

    @Override
    protected void onPause(){
        super.onPause();
        if(mSwitch){
        muzak.release();
        }
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Preferencer pp = (Preferencer)getApplicationContext();
        if(pp.getMuzak()){
            muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
            muzak.setLooping(true);
            muzak.start();
        }
        beginRender();
    }


    public void beginRender(){
        ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
        StateListDrawable background = (StateListDrawable) holmes.getBackground();
        Drawable current = background.getCurrent();
        if(current instanceof AnimationDrawable){
            holmesAnimation = (AnimationDrawable) current;
            holmesAnimation.start();
        }
    }
}
我尝试在Resume上调用
()下的方法
beginRender()
,但随后应用程序崩溃了

谁能给我指一下正确的方向吗

编辑:

我一直在这里和那里调整代码,不幸的是没有用。但我确实注意到动画的行为模式。当我按下
ImageButton
或按住它,使其从默认动画转到按下或聚焦动画,然后将手指从按钮移开,使其不会启动新活动时,它有时的行为与我在本文开头所述的非常相似(也就是说,它应该返回到其默认动画,但却以两倍的速率播放、阻塞或根本不播放。)

目前,包含这些
ImageButton
s的xml将其
背景定义为动画,并且没有源(
src
)。但是当我将
背景更改为透明,将
src
更改为动画时,应用程序崩溃

有什么线索吗