Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 活动在alertDialog show()方法上泄漏了窗口_Android_Android Alertdialog - Fatal编程技术网

Android 活动在alertDialog show()方法上泄漏了窗口

Android 活动在alertDialog show()方法上泄漏了窗口,android,android-alertdialog,Android,Android Alertdialog,由于使用AlertDialog,我在运行时遇到窗口泄漏错误 我已经指出了下面代码中的错误行: Stacktrace: 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.....I

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

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

Stacktrace:

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)
 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
正在运行,而
活动
被销毁。您应该在开始新的
活动之前关闭对话框

.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);
  }
});

我希望有帮助

活动完成后,您必须关闭/取消对话框。如果在活动结束前未取消对话框,则通常会发生此错误。

在onPause()方法中取消alertDialog。i、 e.调用alertDialog.Disclose()


注意:如果在活动结束前未取消对话框,则通常会发生WindowLeaked异常。

我不知道这是否有帮助,但我的问题导致多重索引。 格雷德尔大厦

multiDexEnabled false

如果已设置internet权限,请签入清单:

<uses-permission android:name="android.permission.INTERNET" />

如果您在尝试显示对话框时不在UI线程中,也可以获得WindowLeaked异常。简易修复:

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // show dialog here
    }
});

请检查此项。当您尝试显示/取消在一个活动中创建和显示的ALertDialog,并在另一个活动中取消时,会发生这种情况。请尝试我在回答中编辑的代码。当新活动启动时,关闭对话框或完成活动这意味着您的主线程不知道您的更改,然后当它采取行动时,您的主线程发现它已经是不同的对象。尝试在主线程中调用您的函数。`runOnUiThread(new Runnable(){@Override public void run(){//your function}})`但是在这种情况下,stacktrace应该在
onStop()
onDestroy()
上有漏洞,不是吗?不,多索引在这里不适用=0