我有两个 Activity ,MainActvity和ImageActivity,我的步骤如下:

  • 启动MainActivity
  • 启动ImageActivity
  • 单击backButton返回MainActivity
  • 启动ImageActivity

  • 然后应用崩溃。

    我想原因是因为我回收了imageview,但是这没有任何意义。我不明白为什么它可以在第二个ImageActivity中调用崩溃。有人可以解释吗? (我认为步骤2和步骤4创建了ImageActivity的新实例)

    我的代码如下:

    主要 Activity :
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.nextView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startNewImageActivity();
                }
            });
        }
    
        public void startNewImageActivity(){
            Intent mIntent = new Intent();
            mIntent.setClass(MainActivity.this,ImageActivity.class);
            startActivity(mIntent);
        }
    

    ImageActivity
    public class ImageActivity extends Activity {
        private ImageView mView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.image_layout);
            mView =(ImageView) findViewById(R.id.detail_card_stroke_image);
        }
    
        @Override
        protected void onDestroy() {
    
            Log.i(ImageActivity.class.getName(),"onDestroy");
            if(null == mView){
                return ;
            }
            if(null != mView.getDrawable()){
                if(mView.getDrawable() instanceof BitmapDrawable) {
                    if(null != ((BitmapDrawable) mView.getDrawable()).getBitmap())
                        ((BitmapDrawable) mView.getDrawable()).getBitmap().recycle();
                    // mView.setImageBitmap(null);
                }
            }
    
            super.onDestroy();
        }
    }
    

    image_layout.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/detail_card_stroke_image"
            android:layout_width="354dp"
            android:layout_height="234dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitXY"
            android:src="@drawable/card_glow"
            android:layout_marginTop="10dp"
            android:alpha="1" />
    </LinearLayout>
    

    我的源代码在github

    我的崩溃日志如下:
         11-18 11:36:13.860 21062-21062/com.looppay.android.tpd E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.looppay.android.tpd, PID: 21062
                                                                         java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@5c1dc07
                                                                             at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1301)
                                                                             at android.graphics.Canvas.drawBitmap(Canvas.java:1435)
                                                                             at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:551)
                                                                             at android.widget.ImageView.onDraw(ImageView.java:1252)
                                                                             at android.view.View.draw(View.java:17469)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16464)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.draw(View.java:17472)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16464)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16459)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.draw(View.java:17472)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16464)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16459)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16459)
                                                                             at android.view.View.draw(View.java:17238)
                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
                                                                             at android.view.View.draw(View.java:17472)
                                                                             at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:3205)
                                                                             at android.view.View.updateDisplayListIfDirty(View.java:16464)
                                                                             at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:325)
                                                                             at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:331)
                                                                             at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:366)
                                                                             at android.view.ViewRootImpl.draw(ViewRootImpl.java:3140)
                                                                             at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2939)
                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2522)
                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7414)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:158)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    

    最佳答案

    实际上,这是因为您回收了位图。

    仅当确定在Android 3.0及更高版本上该位图没有更多用途时,才应调用recycle():位图已从内存中重用,并且当您调用recycle()时,已从内存中删除了该位图。

    您可以从ImageView中删除android:src="@drawable/card_glow",并从代码中添加位图:

    Bitmap bitmap = ...;
    if (bitmap != null && !bitmap.isRecycled())
        imageView.setImageBitmap(bitmap);
    

    有关更多信息,请参见Manage Memory

    08-18 16:55