0

我有一个带有 tabHost 的 fragmentActivity。每个 Tab 都有一个在屏幕上显示一些 ButtonTable 的片段。以下代码是 ButtonTable:

public class ButtonTable extends Button {
    private Paint paint= new Paint();
    private Bitmap bmp= null;
    private Table table= null;
    int width= 0;
    int height= 0;
    boolean adjustmentForm= false;
    private WeakReference<Bitmap> resizedBitmap;

    public ButtonTable(Context context, Table table, int width, int height) {
        super(context);
        this.table=table;
        this.width= width;
        this.height= height;
        setWidth(width);
        setHeight(height);
        setBitmapImage();
        setBackgroundColor(INVISIBLE);

    }

   [...]

    public void setBitmapImage(){
        int resurce= R.drawable.rectangle6;

     if(table.getTableType().equals("Circle6")){
         resurce=R.drawable.circle6;
         adjustmentForm= true;
        }
        if(table.getTableType().equals("Circle4")){
            resurce= R.drawable.circle4;
            adjustmentForm= true;
        }

        [...]

        bmp = BitmapFactory.decodeResource(getResources(), resurce);
        if(adjustmentForm){

            bmp = Bitmap.createScaledBitmap(bmp, height, height, true);
        }else{

            bmp = Bitmap.createScaledBitmap(bmp,width, height, true);
        }
        resizedBitmap = new WeakReference<Bitmap>(bmp);


    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);

        if(adjustmentForm){
         canvas.drawBitmap(resizedBitmap.get(), ((width-height)/2), 0, null);
         }else{
            canvas.drawBitmap(resizedBitmap.get(), 0, 0, null);
         }

    }

}

如果我在选项卡中移动一段时间,在大约 10/15 选项卡更改后,应用程序会因为 OutOfMemoryError 而停止。为什么?

4

1 回答 1

0

我解决了我的问题。我在 Fragment 中实现了 onDestroyView()。当您更改选项卡时,片段不会调用 onDestroy()(不会完成其生命周期),而是会调用 onDestroyView(),然后使用 onCreateView() 重新启动。Fragment 从后栈返回布局。 http://developer.android.com/guide/components/fragments.html

@Override
public void onDestroyView() {
    super.onDestroyView();
    layoutHall.removeAllViewsInLayout();

}
于 2013-07-01T13:48:21.217 回答