Java Android动画drwable代码在2.2中运行良好,但在2.3中不工作?

Java Android动画drwable代码在2.2中运行良好,但在2.3中不工作?,java,android,xml,android-manifest,Java,Android,Xml,Android Manifest,我是android新手 我的动画代码在android 2.2中工作正常,但在android 2.3中不工作,当我在android 2.3中运行动画代码时,其显示错误如下: java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@405795e8 在我的活动中,我有三个按钮和一个图像视图,我单击图像视图时,正在播放一个动画,但当我第一次用力打球时,但按钮将播放一些动画,

我是android新手

我的动画代码在android 2.2中工作正常,但在android 2.3中不工作,当我在android 2.3中运行动画代码时,其显示错误如下:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap
android.graphics.Bitmap@405795e8
在我的活动中,我有三个按钮和一个图像视图,我单击图像视图时,正在播放一个动画,但当我第一次用力打球时,但按钮将播放一些动画,到目前为止,它工作正常,但当我再次单击球时,会出现类似log cat中的强制关闭错误

我对此一无所知

我的动画xml文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="true" >

    <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
    <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
 <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
     <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>

    <item
        android:drawable="@drawable/elephant_first"
        android:duration="100"/>
      </animation-list>

我的活动:

public class Home extends Activity implements OnClickListener  {
        Button ball_but,drum_but,hat_but,freeapps_but,moreapps_but,start_but;
        MediaPlayer mp;
        int flag = 0;
        ImageView elepahant_img;
        private static Boolean isSoundPlaying=false;
        static SelectTalkTask mytask;
        AnimationDrawable drawable=null;
        AnimationDrawable drawable_one=null;
        AnimationDrawable drawable_two=null;



        public void onCreate(Bundle savedInstanceState) {        
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
                super.onCreate(savedInstanceState);
                setContentView(R.layout.talking_elephant_home);
                elepahant_img=(ImageView)findViewById(R.id.elephant_img);        
                elepahant_img.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                    isSoundPlaying = true;
                    release(drawable);
                    AnimationDrawable drawable = (AnimationDrawable) getResources().getDrawable(R.layout.water);
                    ImageView im = (ImageView) findViewById(R.id.elephant_img);
                    im.setBackgroundDrawable(drawable);
                    im.post(drawable);
                    play_water(); ///play the water
                    checkIfAnimationDone(drawable);
            }

            private void play_water() {
                    if(mp!=null){
                        mp.reset();
                        mp.release();
                    }
                    mp = MediaPlayer.create(Home.this,R.raw.water);
                    mp.start();
                }
        });

                drum_but=(Button)findViewById(R.id.drum_but);
                drum_but.setOnClickListener(this);

                hat_but=(Button)findViewById(R.id.cap_but);
                hat_but.setOnClickListener(this);

                ball_but=(Button)findViewById(R.id.ball_but);
                ball_but.setOnClickListener(this);


        }


      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ball_but:

            release(drawable);          
             Log.i("TAG", "this is ball_but"+isSoundPlaying);                        
                drawable = (AnimationDrawable) getResources().getDrawable(R.layout.elephant);
                elepahant_img = (ImageView) findViewById(R.id.elephant_img);
                elepahant_img.setBackgroundDrawable(drawable);
                elepahant_img.post(drawable);
                play();
                checkIfAnimationDone(drawable);
                break;                
        case R.id.drum_but:
            isSoundPlaying = true;
            release(drawable);           
             Log.i("TAG", "this is cap_but"+isSoundPlaying);
            drawable = (AnimationDrawable) getResources().getDrawable(R.layout.drum);
            elepahant_img = (ImageView) findViewById(R.id.elephant_img);            
            elepahant_img.setBackgroundDrawable(drawable);
            elepahant_img.post(drawable);
            play_one();
            checkIfAnimationDone(drawable);
            break;
        case R.id.cap_but:
            isSoundPlaying = true;
            release(drawable);           
             Log.i("TAG", "this is cap_but"+isSoundPlaying);
            drawable = (AnimationDrawable) getResources().getDrawable(R.layout.cap);
            elepahant_img = (ImageView) findViewById(R.id.elephant_img);    
            elepahant_img.setBackgroundDrawable(drawable);
            elepahant_img.post(drawable);
            play_two();
            checkIfAnimationDone(drawable);
            break;


        }

    }
      protected void play() {
            if(mp!=null){
                mp.reset();
                mp.release();
            }
            mp = MediaPlayer.create(Home.this,R.raw.ball);
            mp.start();
            }
    private void play_two() {
        if(mp!=null){
            mp.reset();
            mp.release();
        }
        mp = MediaPlayer.create(Home.this,R.raw.cap);
        mp.start();    
    }
    private void play_one() {
        if(mp!=null){
            mp.reset();
            mp.release();
        }
        mp = MediaPlayer.create(Home.this,R.raw.drums);
        mp.start();    
    }

    protected void onPause() {
        super.onPause();
        try{
        mp.stop();
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
     }





     public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                this.finish();            
                System.exit(0);
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }     

        public void onDestroy() { 
            super.onDestroy();
            System.out.println("OnDestroy");             

            System.gc();
            Runtime.getRuntime().gc();
            */
            elepahant_img = (ImageView)findViewById(R.id.elephant_img);
            if (elepahant_img != null)
            {
                ((BitmapDrawable)elepahant_img.getDrawable()).getBitmap().recycle();
                elepahant_img.setImageDrawable(null);
            }
            System.gc();

            }


         public void release(AnimationDrawable ad){
             if(ad==null)
                 return;
             ad.stop();
             for (int i = 0; i < ad.getNumberOfFrames(); ++i){
                 Drawable frame = ad.getFrame(i);
                 if (frame instanceof BitmapDrawable) {

                     ((BitmapDrawable)frame).getBitmap().recycle();
                     }
               frame.setCallback(null);
                 frame=null;
             }
             ad.setCallback(null);
              ad=null;

         }
}   
公共类Home扩展活动实现OnClickListener{
按钮球但是,鼓但是,帽子但是,免费应用但是,更多应用但是,开始但是;
MediaPlayer mp;
int标志=0;
ImageView elepahant_img;
私有静态布尔值isSoundPlaying=false;
静态SelectTalkTaskMyTask;
AnimationDrawable-drawable=null;
AnimationDrawable\u one=null;
AnimationDrawable\u two=null;
创建时的公共void(Bundle savedInstanceState){
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
super.onCreate(savedInstanceState);
setContentView(R.layout.talking\u elephant\u home);
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
elepahant_img.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
isSoundPlaying=正确;
释放(可牵引);
AnimationDrawable drawable=(AnimationDrawable)getResources().getDrawable(R.layout.water);
ImageView im=(ImageView)findViewById(R.id.elephant\u img);
im.可拉伸(可拉伸);
im.post(可提取);
玩水游戏();///玩水游戏
检查是否已完成(可提取);
}
私人空间游戏(水){
如果(mp!=null){
mp.reset();
mp.release();
}
mp=MediaPlayer.create(Home.this,R.raw.water);
mp.start();
}
});
鼓形按钮=(按钮)findViewById(R.id.drum\u but);
drum_but.setOnClickListener(此);
hat_but=(按钮)findviewbyd(R.id.cap_but);
hat_but.setOnClickListener(这个);
ball\u but=(按钮)findViewById(R.id.ball\u but);
ball_but.setOnClickListener(这个);
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.ball_,但:
释放(可牵引);
Log.i(“TAG”,“这是ball_,但“+isSoundPlaying”);
drawable=(AnimationDrawable)getResources().getDrawable(R.layout.elephant);
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
elepahant_img.退根可拉拔(可拉拔);
elepahant_img.post(可牵引);
play();
检查是否已完成(可提取);
打破
案例R.id.drum_,但:
isSoundPlaying=正确;
释放(可牵引);
Log.i(“TAG”,“这是cap_,但是”+isSoundPlaying);
drawable=(AnimationDrawable)getResources().getDrawable(R.layout.drum);
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
elepahant_img.退根可拉拔(可拉拔);
elepahant_img.post(可牵引);
玩一个游戏;
检查是否已完成(可提取);
打破
案例R.id.cap_,但:
isSoundPlaying=正确;
释放(可牵引);
Log.i(“TAG”,“这是cap_,但是”+isSoundPlaying);
drawable=(AnimationDrawable)getResources().getDrawable(R.layout.cap);
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
elepahant_img.退根可拉拔(可拉拔);
elepahant_img.post(可牵引);
玩两个游戏;
检查是否已完成(可提取);
打破
}
}
受保护的无效播放(){
如果(mp!=null){
mp.reset();
mp.release();
}
mp=MediaPlayer.create(Home.this,R.raw.ball);
mp.start();
}
二等兵{
如果(mp!=null){
mp.reset();
mp.release();
}
mp=MediaPlayer.create(Home.this,R.raw.cap);
mp.start();
}
私人空位游戏(一){
如果(mp!=null){
mp.reset();
mp.release();
}
mp=MediaPlayer.create(Home.this,R.raw.drums);
mp.start();
}
受保护的void onPause(){
super.onPause();
试一试{
mp.stop();
}捕获(NullPointerException e){
e、 printStackTrace();
}
}
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if(keyCode==KeyEvent.keyCode\u BACK){
这个;
系统出口(0);
返回true;
}
返回super.onKeyDown(keyCode,event);
}     
公共无效onDestroy(){
super.ondestory();
System.out.println(“OnDestroy”);
gc();
Runtime.getRuntime().gc();
*/
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
if(elepahant_img