尝试显示警告对话框时泄漏窗口

时间:2012-08-29 19:29:19

标签: android alertdialog progressdialog

我正在尝试将iPhone应用程序移植到Android,但我遇到了一些启动困难。

我有以下函数来获取数据:

    public void onClick(View v) {
        try{
            //Toast.makeText(getApplicationContext(), "Starting update", Toast.LENGTH_SHORT).show();
            progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
            new Thread() {
                public void run() {
                    try{
                        DataManager manager = new DataManager(getApplicationContext());
                        manager.updateData();
                    } catch (Exception e) {
                        Log.e(TAG, e.getMessage());

                        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                        alertDialog.setTitle("Error...");
                        alertDialog.setMessage(e.getMessage());
                        alertDialog.show();
                    }
                    progressDialog.dismiss();
                }
            }.start();
        } catch (Exception e) {
            Log.e(TAG, "Problem with getting the data: " + e.getMessage());
            Toast.makeText(getApplicationContext(), "Error during update",
                    Toast.LENGTH_SHORT).show();
        }

    }

但是如果发生异常,我会收到以下错误(我最初尝试使用AsyncTask,但遇到了同样的问题):

08-29 20:23:24.389: ERROR/WindowManager(974): Activity org.idoms.iDomsAndroid.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412fcc38 that was originally added here
        android.view.WindowLeaked: Activity org.idoms.iDomsAndroid.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412fcc38 that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
        at android.view.Window$LocalWindowManager.addView(Window.java:537)
        at android.app.Dialog.show(Dialog.java:278)
        at android.app.ProgressDialog.show(ProgressDialog.java:116)
        at android.app.ProgressDialog.show(ProgressDialog.java:99)
        at android.app.ProgressDialog.show(ProgressDialog.java:94)
        at org.idoms.iDomsAndroid.MainActivity$1.onClick(MainActivity.java:46)
        at android.view.View.performClick(View.java:3511)
        at android.view.View$PerformClick.run(View.java:14105)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

我的猜测是,线程无法与主UI线程元素交互。

progressDialog是在主Ui线程中创建的,其他线程无法与之交互。你可以在字段中给它一个信号并检查它的进度,但我真的建议使用Asynctask来完成这个任务。

AsyncTask

答案 1 :(得分:0)

UI元素(如AlertDialog)只能从主(UI)线程中操作。如果您尝试从后台线程操作它们(这是您使用new Thread() { ... }执行的操作),您将获得此异常。

您可以在访问runOnUiThread(new Runnable() { ... });AlertDialog时致电progressDialog来解决此问题,但这非常非常非常脏

更好的解决方案是使用AsyncTask(甚至更好AsyncTaskLoader,因为它会照顾方向更改和其他一些东西)。使用AsyncTask时,请确保访问doInBackground() - 方法中的UI元素。作为名称,saysdoInBackground(...)在后​​台线程中执行。如果您尝试从该方法操作UI元素,您确实会遇到与您现在遇到的相同的错误。如果您想从AsyncTask访问您的UI元素,可以使用onPreExecute(...)onProgressUpdate(...)onPostExecute(...)方法执行此操作。

答案 2 :(得分:0)

窗口泄漏的原因是由于您的alertDialog在应用关闭之前没有被解雇。