展示吐司后,活动已经泄露了窗口

时间:2015-04-17 15:18:28

标签: android window android-toast

我试图检测用户是否在其设备上激活了互联网连接,如果它未连接到互联网,则应该将用户带到“设置”应用激活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中还是其他任何东西?

2 个答案:

答案 0 :(得分:0)

如果您在退出活动后尝试显示对话框等,则会发生此错误。

在退出活动之前,您必须在您创建的对话框上调用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

答案 1 :(得分:0)

试试这个:

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();

}