0

I'm trying to detect whether the user has an Internet connection activated on their device, and if it's not connected to the Internet, it's supposed to take the user to the Settings app to activate wi-fi/mobile data.

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

However, I get this long litany of errors:

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)

I've seen many solutions when it's a Dialog causing the problem, but nothing specific to Toasts. If it helps, that bit of code is in the onCreate() method, should it be in an AsyncTask or anything else instead?

CiaranC94
  • 176
  • 4
  • 20

2 Answers2

0

This error occurs if you are trying to show dialog etc after you have exited an Activity.

You must call dismiss() on the dialog you created in before exiting the activity.

For example see this code

@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;
        }
    }

Post complete code so that I can tell you what exactly is wrong in code

See these links for help

Activity has leaked window that was originally added

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

Community
  • 1
  • 1
0

Try this:

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

}
ThemuRR
  • 61
  • 1
  • 13