ProgressDialog错误...泄漏的窗口

时间:2013-09-23 15:26:23

标签: java android

我有一个进度对话框,当它完成计数以开始一项活动时我需要。但是我收到了一个错误。所以,这是我的进度对话框方法:

private void myProgress(){
        progressBar = new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.setMessage("Hacking the profile ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();
        progressBarStatus = 0;
        fileSize = 0;

        new Thread(new Runnable() {
              public void run() {
                while (progressBarStatus < 100) {

                  // process some tasks
                  progressBarStatus = doSomeTasks();


                  try {
                    Thread.sleep(500);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }

                  // Update the progress bar
                  progressBarHandler.post(new Runnable() {
                    public void run() {
                      progressBar.setProgress(progressBarStatus);
                    }
                  });
                }

                // progress 100%
                if (progressBarStatus >= 100) {

                    // sleep 2 seconds, so that you can see the 100%
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // Start Result activity
                    Intent localIntent = new Intent(MainActivity.this, Result.class);
                    localIntent.putExtra("email", emailText.getText().toString());
                    startActivity(localIntent);
                }
              }
               }).start();

               }

doSomeTasks方法:

public int doSomeTasks() {

        while (fileSize <= 1000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }
            else if (fileSize == 400000) {
                return 40;
            }
            else if (fileSize == 500000) {
                return 50;
            }
            else if (fileSize == 600000) {
                return 60;
            }
            else if (fileSize == 700000) {
                return 70;
            }
            else if (fileSize == 800000) {
                return 80;
            }
            else if (fileSize == 900000) {
                return 90;
            }

        }

        return 100;

    }

我在这一行上收到错误:

 localIntent.putExtra("email", emailText.getText().toString());

logcat的:

09-23 14:47:20.946: E/WindowManager(1497): Activity com.myapps.someapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@a6aa84f0 that was originally added here
09-23 14:47:20.946: E/WindowManager(1497): android.view.WindowLeaked: Activity com.myapps.someapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@a6aa84f0 that was originally added here
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
09-23 14:47:20.946: E/WindowManager(1497):  at android.app.Dialog.show(Dialog.java:277)
09-23 14:47:20.946: E/WindowManager(1497):  at com.myapps.someapp.MainActivity.hackAccount(MainActivity.java:67)
09-23 14:47:20.946: E/WindowManager(1497):  at com.myapps.someapp.MainActivity.access$0(MainActivity.java:60)
09-23 14:47:20.946: E/WindowManager(1497):  at com.myapps.someapp.MainActivity$1.onClick(MainActivity.java:54)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.View.performClick(View.java:4084)
09-23 14:47:20.946: E/WindowManager(1497):  at android.view.View$PerformClick.run(View.java:16966)
09-23 14:47:20.946: E/WindowManager(1497):  at android.os.Handler.handleCallback(Handler.java:615)
09-23 14:47:20.946: E/WindowManager(1497):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 14:47:20.946: E/WindowManager(1497):  at android.os.Looper.loop(Looper.java:137)
09-23 14:47:20.946: E/WindowManager(1497):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-23 14:47:20.946: E/WindowManager(1497):  at java.lang.reflect.Method.invokeNative(Native Method)
09-23 14:47:20.946: E/WindowManager(1497):  at java.lang.reflect.Method.invoke(Method.java:511)
09-23 14:47:20.946: E/WindowManager(1497):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-23 14:47:20.946: E/WindowManager(1497):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-23 14:47:20.946: E/WindowManager(1497):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

AsyncTask似乎更适合您的需要:

fileSize = 0;
private class HackingTask extends
        AsyncTask<Void, Integer, Void> {

    ProgressDialog progressDialog;

    // Before running code in separate thread
    @Override
    protected void onPreExecute() {
         progressDialog= new ProgressDialog(this);
         progressDialog.setCancelable(true);
         progressDialog.setMessage("Hacking the profile ...");
         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         progressDialog.setProgress(0);
         progressDialog.setMax(100);
         progressDialog.show();
    }

    // The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... arg) {

              while (progressDialog.getProgress() < 100) {
                  publishProgress(doSomeTasks());

                  try {
                     Thread.sleep(500);
                   } catch (InterruptedException e) {
                     e.printStackTrace();
                   }
              }
    }

    // Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        // set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(Void... arg) {
        // close the progress dialog
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

    }
}

并运行它:

new HackingTask().execute();

答案 1 :(得分:0)

错误说,您正在尝试打开已关闭的活动的对话框。见here

在显示ProgressBar时,您的活动是否正在运行?