试图使用回收的位图android问题

时间:2014-12-26 07:00:19

标签: android bitmap imageview android-imageview android-image

Main.java

public class Main extends Activity{



ImageView mImageViewAddIncome;
ImageView mImageViewLogo;
ImageView mImageViewSetting;
ImageView mImageViewBack;
ImageView mImageViewSave;
RelativeLayout mRelativeLayoutRoot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_income);
    mImageViewAddIncome=(ImageView)findViewById(R.id.header_add_icon);
            mImageViewBack=(ImageView)findViewById(R.id.img_back);
    mImageViewLogo=(ImageView)findViewById(R.id.logo);
    mImageViewSetting=(ImageView)findViewById(R.id.settting);
    mImageViewSave=(ImageVIew)findviewById(R.id.save);
    mRelativeLayoutRoot=(RelativeLayout)findViewById(R.id.inner_content);


     }





        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            System.out.println("Destroy");




            recycleBitmapFromView(mImageViewAddIncome);
            recycleBitmapFromView(mImageViewBack);
            recycleBitmapFromView(mImageViewLogo);
            recycleBitmapFromView(mImageViewSave);
             recycleBitmapFromView(mImageViewSetting);



             recycleBitmapFromView(mRelativeLayoutRoot);


        }



      }

recycleBitmapFromView方法

public static void recycleBitmapFromView(View view) {
    if(view instanceof ImageView)
    {
        Drawable drawable = ((ImageView)view).getDrawable();

        if(drawable instanceof BitmapDrawable)
        {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                Bitmap bitmap = bitmapDrawable.getBitmap();
                bitmap.recycle();
        }
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            recycleBitmapFromView(((ViewGroup) view).getChildAt(i));
        }
    }
}

修改

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inner_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/rel_top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/header_bg" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@drawable/drower_logo" />

    <TextView
        android:id="@+id/titel"
        style="@style/pro_text_white"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:text="Income"
        android:textSize="@dimen/text_18" />

    <ImageView
        android:id="@+id/settting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@drawable/setting"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/txt_rent_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@drawable/save_icon"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/header_add_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@drawable/addnew_tenant"
        android:visibility="gone" />
</RelativeLayout>

<View
    android:id="@+id/v"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/rel_top"
    android:background="@color/bottom_line"
    android:visibility="gone" />

<RelativeLayout
    android:id="@+id/rel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/v"
    android:background="@color/drawer_top_bg" >

    <RelativeLayout
        android:id="@+id/rel_draw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/sample_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_list"
            android:visibility="visible" />

        <ImageView
            android:id="@+id/dr_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/sample_button"
            android:padding="10dp" />

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:padding="10dp"
            android:src="@drawable/icon_back" />
    </RelativeLayout>
</RelativeLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rel"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:cacheColorHint="@android:color/transparent"
    android:divider="@android:color/transparent"
    android:dividerHeight="5.0sp"
    android:visibility="visible" >
</ListView>



<TextView
    android:id="@+id/txt_sub_pro"
    style="@style/pro_text_white_header"
    android:layout_below="@+id/list_main"
    android:layout_marginTop="10dp"
    android:drawableRight="@drawable/setting_arrow"
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:text=""
    android:visibility="gone" />

<ListView
    android:id="@+id/list_child"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_sub_pro"
    android:cacheColorHint="@android:color/transparent"
    android:divider="@android:color/transparent"
    android:dividerHeight="5.0sp"
    android:visibility="gone" >
    </ListView>


    </RelativeLayout>

你好朋友,当我在鳕鱼之上运行时给了我错误,如

 Canvas: trying to use a recycled bitmap android.graphics.Bitmap@421b5b38

任何想法我该如何解决?

1 个答案:

答案 0 :(得分:0)

在代码中替换此行..并尝试运行

bitmap.recycle();

if (bitmap!= null && !bitmap.isRecycled()) {
    bitmap.recycle();
    bitmap= null; 
}

如果没有帮助,请检查此https://stackoverflow.com/a/7937945/3156621