1

我是安卓新手。

我的动画代码在 android 2.2 中运行良好,但在 android 2.3 中无法运行,当我在 android 2.3 中运行动画代码时,其显示错误如下:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap
android.graphics.Bitmap@405795e8

在我的活动中,我有三个按钮和一个图像视图,其中我单击图像视图正在播放一个动画但是当我第一次使用 ball_but 按钮时它会播放一些动画到现在它工作正常但是当我再次单击 ball_but 时在 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;

         }
}   
4

1 回答 1

0

当您尝试回收已回收的位图时,您可能会收到此错误。在框架的位图上调用回收之前添加一个空测试。

于 2012-08-14T11:30:18.233 回答