为什么我在尝试在自定义类中显示对话框时获得WindowLeaked?

时间:2013-03-07 00:13:03

标签: android android-alertdialog

我正在尝试创建一个AlertDialog,我已经完成了这一百次,但这次它不起作用。我的活动中的onResume()方法中包含此代码:

@Override
protected void onResume() {
    super.onResume();
    if (DEBUG)
        Log.i("BrazoRobot", "onResume()");
    isSystemRdy = false;

    mBluetoothHelper = new BluetoothHelper(this, bluetoothName);
    mBluetoothHelper.connect();
    mBluetoothHelper.setOnBluetoothConnected(this);
}

正确创建了不是Activity类的对象BluetoothHelper,我将活动上下文传递给它:

public BluetoothHelper (final Context ctx, final String bluetoothName) {
    mActivity = ((Activity)ctx);
    this.ctx = ctx;
    this.bluetoothName = bluetoothName;
}

问题在于我创建对话框的connect()方法:

if (!mBluetoothAdapter.isEnabled()) {
    AlertDialog.Builder mDialog = new AlertDialog.Builder(ctx);
    mDialog.setTitle(ctx.getString(R.string.BTRequestTitle));
    mDialog.setMessage(ctx.getString(R.string.BTRequestSummary));
    mDialog.setPositiveButton(ctx.getString(R.string.Yes), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (DEBUG)
                Log.i("BrazoRobotBT", "Turning on Bluetooth...");
            mBluetoothAdapter.enable();     // Enciendo el Bluetooth
        }
    });
    mDialog.setNegativeButton(ctx.getString(R.string.No), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (DEBUG)
                Log.i("BrazoRobotBT", "Exit");
            mActivity.finish();
        }
    });
    mDialog.show();
}

我在mDialog.show()收到以下异常。 ctx是我在构造函数中传递的活动上下文,我也尝试将mActivity ((Activity) ctx)传递给AlertDialog.Builder,但我得到了同样的例外。

对话框是要求用户启用蓝牙。我知道正确的方法是使用内置方法调用startActivityForResult()但是我希望将所有内容都放在我的自定义类中,如果它不是一个Activity,我就不能在我的类上使用onActivityResult()为什么我这样做。

为什么会这样? AFAIK我在UI线程上创建对话框。我也尝试在onCreate()方法中创建对话框,但它仍然是相同的。

03-06 21:06:20.320: E/WindowManager(31411): Activity com.roboticarm.andres.BrazoRobot has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4130de20 V.E..... R.....ID 0,0-495,244} that was originally added here
03-06 21:06:20.320: E/WindowManager(31411): android.view.WindowLeaked: Activity com.roboticarm.andres.BrazoRobot has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4130de20 V.E..... R.....ID 0,0-495,244} that was originally added here
03-06 21:06:20.320: E/WindowManager(31411):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
03-06 21:06:20.320: E/WindowManager(31411):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
03-06 21:06:20.320: E/WindowManager(31411):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.Dialog.show(Dialog.java:281)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
03-06 21:06:20.320: E/WindowManager(31411):     at com.bluetoothutils.andres.BluetoothHelper.connect(BluetoothHelper.java:119)
03-06 21:06:20.320: E/WindowManager(31411):     at com.roboticarm.andres.BrazoRobot.onResume(BrazoRobot.java:247)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.Activity.performResume(Activity.java:5217)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2862)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2901)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2364)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-06 21:06:20.320: E/WindowManager(31411):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 21:06:20.320: E/WindowManager(31411):     at android.os.Looper.loop(Looper.java:137)
03-06 21:06:20.320: E/WindowManager(31411):     at android.app.ActivityThread.main(ActivityThread.java:5204)
03-06 21:06:20.320: E/WindowManager(31411):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 21:06:20.320: E/WindowManager(31411):     at java.lang.reflect.Method.invoke(Method.java:511)
03-06 21:06:20.320: E/WindowManager(31411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
03-06 21:06:20.320: E/WindowManager(31411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-06 21:06:20.320: E/WindowManager(31411):     at dalvik.system.NativeStart.main(Native Method)

编辑1(onWindowFocusChanged)

按照建议尝试此代码:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        // Conecto al dispositivo bluetooth
        mBluetoothHelper = new BluetoothHelper(this, "linvor");
        mBluetoothHelper.connect();
        if(DEBUG) Log.i("BrazoRobotBT", "Interface");
        mBluetoothHelper.setOnNewBluetoothDataReceived(this);
        setPreferences();

        // Indico que entro en el Analizador lógico
        mBluetoothHelper.write(logicAnalyzerMode);
    }
}

但我一直得到完全相同的错误。


编辑2(活动测试)

我尝试在Activity中的onCreate()和onResume()中创建一个Dialog,并使用这个简单的代码完美地创建它:

    public class CustomDialog extends Activity{

        private static final boolean DEBUG = true;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            AlertDialog.Builder mDialog = new AlertDialog.Builder(this);
            mDialog.setTitle(getString(R.string.BTRequestTitle));
            mDialog.setMessage(getString(R.string.BTRequestSummary));

            mDialog.setPositiveButton(getString(R.string.Yes), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Turning on Bluetooth...");
                }
            });
            mDialog.setNegativeButton(getString(R.string.No), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Exit");
                }
            });
            mDialog.show();
        }
    }

But, if I put this code inside my class method called from my Activity I get the exception.

编辑3(奇怪事实)

这是我的connect()方法,当我从我的Activity中调用它时会出现异常,并显示对话框:

public void connect (){
        // Compruebo que el dispositivo tenga Bluetooth
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Si no hay Bluetooth en el dispositivo muestro un dialogo alertando al usuario y salgo de la Activity
            AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
            dialog.setTitle(ctx.getString(R.string.NoBTAlertTitle));
            dialog.setMessage(ctx.getString(R.string.NoBTAlertText));
            dialog.setPositiveButton(ctx.getString(R.string.Ok), new OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mActivity.finish(); // Cierro porque no existe un módulo Bluetooth
                }
            });
        }
        // Si el dispositivo tiene Bluetooth me conecto
        else{
            // Compruebo que el Bluetooth esté activado, sino pido al usuario que lo active
            if (!mBluetoothAdapter.isEnabled()) {

                //mActivity.startActivity(new Intent(mActivity, CustomDialog.class));

                AlertDialog.Builder mDialog = new AlertDialog.Builder(ctx);
                mDialog.setTitle(ctx.getString(R.string.BTRequestTitle));
                mDialog.setMessage(ctx.getString(R.string.BTRequestSummary));

                mDialog.setPositiveButton(ctx.getString(R.string.Yes), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(DEBUG) Log.i("BrazoRobotBT", "Turning on Bluetooth...");
                        mBluetoothAdapter.enable();     // Enciendo el Bluetooth
                    }
                });
                mDialog.setNegativeButton(ctx.getString(R.string.No), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(DEBUG) Log.i("BrazoRobotBT", "Exit");
                        mActivity.finish();
                    }
                });
                mDialog.show();
            }
            // Compruebo si el dispositivo no esta en los dispositivos emparejados (paired)
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                // Loop a travez de los dispositivos emparejados (paired)
                for (BluetoothDevice device : pairedDevices) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Name: " + device.getName() + " -- Address:  " + device.getAddress());
                    // Si el dispositivo coincide con el que busco lo asigno
                    if(device.getName().equals(bluetoothName)){
                        mBluetoothDevice = device;
                        // Establezco una conexión Bluetooth para enviar datos
                        establishConnection();
                        break;
                    }
                }
            }
            // Sino salgo, debe estar en los dispositivos emparejados
            else{
                mActivity.finish();
            }
        }
    }

但是如果我创建一个单独的方法并且也从Activity调用,那么在显示Dialog时我没有得到异常:

public void dialog(){
    AlertDialog.Builder mDialog = new AlertDialog.Builder(ctx);
    mDialog.setTitle(ctx.getString(R.string.BTRequestTitle));
    mDialog.setMessage(ctx.getString(R.string.BTRequestSummary));

    mDialog.setPositiveButton(ctx.getString(R.string.Yes), new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(DEBUG) Log.i("BrazoRobotBT", "Turning on Bluetooth...");
        }
    });
    mDialog.setNegativeButton(ctx.getString(R.string.No), new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(DEBUG) Log.i("BrazoRobotBT", "Exit");
            mActivity.finish();
        }
    });
    mDialog.show();
}

并使用它:

mCustomClass.dialog();      // Dialog is shown, no exception

2 个答案:

答案 0 :(得分:0)

为了避免此错误,您应该在活动的 onStop 方法中调用mDialog.dismiss()

原因:因为你不能简单地完成对话框所有者活动,或者在没有正确处理对话窗口的情况下跳转到新活动。

答案 1 :(得分:0)

好好休息一下后我解决了。我的错了。问题基本上是我出现了对话框,在显示之前我完成了活动:

public void connect (){
        // Compruebo que el dispositivo tenga Bluetooth
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Si no hay Bluetooth en el dispositivo muestro un dialogo alertando al usuario y salgo de la Activity
            AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
            dialog.setTitle(ctx.getString(R.string.NoBTAlertTitle));
            dialog.setMessage(ctx.getString(R.string.NoBTAlertText));
            dialog.setPositiveButton(ctx.getString(R.string.Ok), new OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mActivity.finish(); // Cierro porque no existe un módulo Bluetooth
                }
            });
        }
        // Si el dispositivo tiene Bluetooth me conecto
        else{
            // Compruebo que el Bluetooth esté activado, sino pido al usuario que lo active
            if (!mBluetoothAdapter.isEnabled()) {

                //mActivity.startActivity(new Intent(mActivity, CustomDialog.class));

                AlertDialog.Builder mDialog = new AlertDialog.Builder(ctx);
                mDialog.setTitle(ctx.getString(R.string.BTRequestTitle));
                mDialog.setMessage(ctx.getString(R.string.BTRequestSummary));

                mDialog.setPositiveButton(ctx.getString(R.string.Yes), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(DEBUG) Log.i("BrazoRobotBT", "Turning on Bluetooth...");
                        mBluetoothAdapter.enable();     // Enciendo el Bluetooth
                    }
                });
                mDialog.setNegativeButton(ctx.getString(R.string.No), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(DEBUG) Log.i("BrazoRobotBT", "Exit");
                        mActivity.finish();
                    }
                });
                mDialog.show();
            }
            // Compruebo si el dispositivo no esta en los dispositivos emparejados (paired)
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                // Loop a travez de los dispositivos emparejados (paired)
                for (BluetoothDevice device : pairedDevices) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Name: " + device.getName() + " -- Address:  " + device.getAddress());
                    // Si el dispositivo coincide con el que busco lo asigno
                    if(device.getName().equals(bluetoothName)){
                        mBluetoothDevice = device;
                        // Establezco una conexión Bluetooth para enviar datos
                        establishConnection();
                        break;
                    }
                }
            }
            // Sino salgo, debe estar en los dispositivos emparejados
            else{
                mActivity.finish();
            }
        }
    }

我在'mDialog.show()上显示对话框但是蓝牙仍然没有启用,直到我按下确定按钮,所以没有配对的设备,所以我完成了活动,但对话框仍在绘制,所以我得到了例外。我知道愚蠢的错误,有时我应该休息一下。

所以'connect()'方法现在是:

public void connect (){
    if(DEBUG) Log.i("BrazoRobotBT", "connect()...");
    // Compruebo que el dispositivo tenga Bluetooth
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Si no hay Bluetooth en el dispositivo muestro un dialogo alertando al usuario y salgo de la Activity
        AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
        dialog.setTitle(ctx.getString(R.string.NoBTAlertTitle));
        dialog.setMessage(ctx.getString(R.string.NoBTAlertText));
        dialog.setPositiveButton(ctx.getString(R.string.Ok), new OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(DEBUG) Log.i("BrazoRobotBT", "No bluetooth on device");
                mActivity.finish(); // Cierro porque no existe un módulo Bluetooth
            }
        });
    }
    // Si el dispositivo tiene Bluetooth me conecto
    else{
        // Compruebo que el Bluetooth esté activado, sino pido al usuario que lo active
        if (!mBluetoothAdapter.isEnabled()) {
            final AlertDialog.Builder mDialog = new AlertDialog.Builder(ctx);
            mDialog.setTitle(ctx.getString(R.string.BTRequestTitle));
            mDialog.setMessage(ctx.getString(R.string.BTRequestSummary));

            mDialog.setPositiveButton(ctx.getString(R.string.Yes), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Turning on Bluetooth...");
                    mBluetoothAdapter.enable();     // Enciendo el Bluetooth

                    // Espero a que encienda el Bluetooth
                    while(!mBluetoothAdapter.isEnabled());

                    // Compruebo si el dispositivo no esta en los dispositivos emparejados (paired)
                    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                    if (pairedDevices.size() > 0) {
                        // Loop a travez de los dispositivos emparejados (paired)
                        for (BluetoothDevice device : pairedDevices) {
                            if(DEBUG) Log.i("BrazoRobotBT", "Name: " + device.getName() + " -- Address:  " + device.getAddress());
                            // Si el dispositivo coincide con el que busco lo asigno
                            if(device.getName().equals(bluetoothName)){
                                mBluetoothDevice = device;
                                // Establezco una conexión Bluetooth para enviar datos
                                establishConnection();
                                break;
                            }
                        }
                    }
                    // Sino salgo, debe estar en los dispositivos emparejados
                    else{
                        if(DEBUG) Log.i("BrazoRobotBT", "Finish Activity not in paired devices");
                        mBluetoothAdapter.disable();
                        mActivity.finish();
                    }
                }
            });

            mDialog.setNegativeButton(ctx.getString(R.string.No), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(DEBUG) Log.i("BrazoRobotBT", "Exit");
                    mActivity.finish();
                }
            });

            mDialog.show();
        }
    }
}

它正常运作并且符合预期。非常感谢Waqas的帮助,但我在AlertDialog.Builder上没有'mDialog.dismiss()'方法,因为它是自动完成的。