H5W3
当前位置:H5W3 > 其他技术问题 > 正文

在destroy中移出浮动框的时候报错

这是报错信息 android.view.WindowLeaked: Activity com.example.ce.MainActivity has leaked window android.widget.ImageView{50f36bf V.ED…. …….. 0,0-144,144} that was originally added here

一下是浮动框代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                
            }
        });
        
        display = getWindowManager().getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();
        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        image = new ImageView(getBaseContext());
        image.setBackgroundResource(R.drawable.ic_launcher);
        
        mparams = new WindowManager.LayoutParams();
        mparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mparams.type = WindowManager.LayoutParams.TYPE_PHONE;
        mparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mparams.gravity = PixelFormat.RGBA_8888;
        mparams.gravity = Gravity.TOP | Gravity.LEFT;
        mparams.x = (int) (width/2);
        mparams.y = (int) (height/2);
        wm.addView(image, mparams);
        
    }
    
    public boolean remove(){
        wm.removeView(image);
        return true;
    }
@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (isFinishing()) {
            Log.i("TETS", "remove");
            remove();
            Log.i("TEST", "after remove");
        }else{
            Log.i("TEST", "activity is close");
        }
        super.onDestroy();
        
    }

打出的log是remove那边而且也打出了after remove 然后就出现错误信息了这是什么情况啊 我在stop中调用remove的时候也不会出现这个错误啊 只有在destroy中调用remove才会出现错误 该怎么解决啊

回答:

ondestory 的时候 activity 已经销了,但ImageView 没有被注销掉
你把ImageView 设为null 试试,没有过环境没法帮你试

本文地址:H5W3 » 在destroy中移出浮动框的时候报错

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址