android - Activity 在 alertDialog show() 方法处泄漏了窗口

标签 android android-alertdialog

由于使用 AlertDialog,我在运行时遇到窗口泄漏错误。

我已经指出了下面代码中的错误行:

堆栈跟踪:

08-18 02:48:04.489  28893-28893/? E/WindowManager﹕ Activity com.ms.ha.fragment.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e58540 V.E..... R.....ID 0,0-1026,585} that was originally added here
    android.view.WindowLeaked: Activity com.ms.ha.fragment.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e58540 V.E..... R.....ID 0,0-1026,585} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at com.ms.ha.fragment.TourGuideLoadURLFragment$WebAppInterface.moveToNextScreen(TourGuideLoadURLFragment.java:116)
            at android.webkit.WebViewCore.nativeMouseClick(Native Method)
            at android.webkit.WebViewCore.nativeMouseClick(Native Method)
            at android.webkit.WebViewCore.access$6800(WebViewCore.java:59)
            at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1793)
            at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)
            at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)
            at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)
            at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:814)
            at java.lang.Thread.run(Thread.java:841)

FirstActivity.java:

 public class FirstActivity extends FragmentActivity  implements View.OnClickListener{

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_tour_guide_web);
  .......
  .......

   webView.addJavascriptInterface(new WebAppInterface(this), "Android");

 }


    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /**
         * Intent - Move to next screen
         */

        @JavascriptInterface
        public void moveToNextScreen() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Click yes!")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
                            startActivity(i);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();  --->leak window error

        }

    }

  }

我不知道如何解决这个问题。

最佳答案

您收到错误是因为 ProgressDialog 正在运行,而您的 Activity 被销毁。您应该在开始新的 Activity 之前关闭对话框。

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
      if (alertDialog != null && alertDialog.isShowing()) {
          alertDialog.dismiss();
      }
      Intent i = new Intent(FirstActivity.this, SecondActivity.class);
      startActivity(i);
  }
});

希望对你有帮助!

关于android - Activity 在 alertDialog show() 方法处泄漏了窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065854/

相关文章:

android - 如何从android中的视频中获取帧数?

javascript - 构建后,Ionic App CSS 样式未在 Android 设备中正确显示

android - NotificationChannel 通知作为电话静音时的警报(奥利奥)

java - 使对话框保持在顶部

Android:使用ADB获取所有已安装的包

java - 从 onPostExecute 内部 AsyncTask 获取结果到 OnCreate

安卓| RecyclerView 项目上的 AlertDialog 单击

java - 将取消按钮添加到自定义警报对话框

java - Android - AlertDialog 中的字符串值在访问时不断重置

android - 在 Android 中将参数传递给 AlertDialog