java - 在destroy中移出浮动框的时候报错
PHP中文网
PHP中文网 2017-04-17 17:41:23
0
1
337

这是报错信息 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才会出现错误 该怎么解决啊

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
Peter_Zhu

Ondestory the activity has been canceled, but the ImageView has not been logged out
You can try setting ImageView to null. I can’t help you try without the environment

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template