活动在显示 Toast 后泄漏窗口 [重复]

Posted

技术标签:

【中文标题】活动在显示 Toast 后泄漏窗口 [重复]【英文标题】:Activity has leaked window after showing a Toast [duplicate] 【发布时间】:2015-06-24 12:59:24 【问题描述】:

我正在尝试检测用户是否在其设备上激活了 Internet 连接,如果未连接到 Internet,则应该将用户带到“设置”应用以激活 wi-fi/移动数据。

if(!HelperFunctions.hasInternetAccess(this))
        Toast.makeText(this, "You are not connected to the Internet. Please connect to wi-fi or activate your mobile data.", Toast.LENGTH_LONG).show();
        startActivity(new Intent(Settings.ACTION_SETTINGS));
        

但是,我收到了这么多错误:

Activity com.[packageName].scanner.ReadCardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42c74c40 that was originally added here
    android.view.WindowLeaked: Activity com.[packageName].scanner.ReadCardActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42c74c40 that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
            at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
            at android.view.Window$LocalWindowManager.addView(Window.java:554)
            at android.app.Dialog.show(Dialog.java:277)
            at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
            at com.touchtechpayments.scanner.ReadCardActivity.onCreate(ReadCardActivity.java:83)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            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:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)

当是对话框导致问题时,我已经看到了许多解决方案,但没有特定于 Toasts。如果有帮助,那段代码在 onCreate() 方法中,它应该在 AsyncTask 中还是其他任何东西中?

【问题讨论】:

Toast 没有引起这个异常...... 如果您认为它与Dialog 无关,请在ReadCardActivity.java 中发布您的onCreate() 并注明第83 行。 这是第 83 行:alert.show(); 【参考方案1】:

如果您在退出 Activity 后尝试显示对话框等,则会出现此错误。

您必须在退出活动之前在您创建的对话框中调用dismiss()

例如看这段代码

@Override
   public void onClick(View v) 
    switch (v.getId()) 
        case R.id.new_button:
            openMyAlertDialog();
            break; <-- If you forget this the finish() method below 
                       will be called while the dialog is showing!
        case R.id.exit_button:
            finish();
            break;
        
    

发布完整的代码,以便我可以告诉你代码中到底有什么问题

查看这些链接获取帮助

Activity has leaked window that was originally added

android activity has leaked window com.android.internal.policy.impl.phonewindow$decorview Issue

【讨论】:

【参考方案2】:

试试这个:

private boolean isNetworkAvailable()

    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();


if (!isNetworkAvailable())

        Toast.makeText(this, "You are not connected to the Internet. Please connect to wi-fi or activate your mobile data.", Toast.LENGTH_LONG).show();
    startActivity(new Intent(Settings.ACTION_SETTINGS));Toast.LENGTH_LONG).show();


【讨论】:

但是,如果我的网络数据/wi-fi 连接速度非常慢怎么办?

以上是关于活动在显示 Toast 后泄漏窗口 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

TOAST:不重复显示Toast

怎么解决toast连续弹出的问题

Android 特殊的单例Toast(防止重复显示)

Toast 消息未在模拟器像素 3a 中显示 [重复]

主 Activity 完成后如何从服务中显示 Toast?

防止 Ionic 中出现重复的 Toast 消息