boolean android.net.NetworkInfo.isConnectedOrConnecting() is not working in Android 5.1.1

扶醉桌前 提交于 2020-04-15 17:12:47

问题


I am working on an Android Tablet Application, I am checking Internet connectivity within an Activity named "Home". I have tested the app in Android version 4.2 (Tablet), 4.4 (Tablet), 5.0 (Phone), 5.0.2 (Phone), the app is working fine.

Problem: When I tested the app in Android 5.1.1 (Tablet), app is being forced close. Its showing following error in log.

Log:

2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freestyle/com.freestyle.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.access$800(ActivityThread.java:151)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.os.Handler.dispatchMessage(Handler.java:102)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.os.Looper.loop(Looper.java:135)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.main(ActivityThread.java:5254)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at java.lang.reflect.Method.invoke(Native Method)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at java.lang.reflect.Method.invoke(Method.java:372)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.freestyle.utils.NetworkHelper.isConnectingToInternet(NetworkHelper.java:20)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.freestyle.Home.onCreate(Home.java:332)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.Activity.performCreate(Activity.java:5990)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : ... 10 more
2015-07-27 05:36:19.313 WARN:   ActivityManager : Force finishing activity 1 com.freestyle/.Home
2015-07-27 05:36:19.367 INFO:   OpenGLRenderer : Initialized EGL, version 1.4
2015-07-27 05:36:19.369 DEBUG:  mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.383 DEBUG:  mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.817 WARN:   ActivityManager : Activity pause timeout for ActivityRecord{2c5a4722 u0 com.freestyle/.Home t22999 f}

Method to check Internet Connectivity:

public boolean isConnectingToInternet() {
        boolean status = false;

            ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()
                    && cm.getActiveNetworkInfo().isAvailable()
                    && cm.getActiveNetworkInfo().isConnected()) {
                //have to assign true
                status = true;
                return status;
            }

        return status;
    }

Code of Home Activity:

public class Home extends Activity implements OnClickListener {

    NetworkHelper nh = null;

@Override
    protected void onCreate(Bundle savedInstanceState) {

    nh = new NetworkHelper(HomeActivity.this);

    if (nh.isConnectingToInternet()) {
            ApplicationUpdates app = new ApplicationUpdates(HomeActivity.this,
                    loadingString, updateMessage);
            app.fetchUpdate();
        }
    }
}

回答1:


check this way. Its work fine for mw

public static boolean checkInternetConnection(Context context)
    {
        try
        {
            ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
                return true;
            else
                return false;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }



回答2:


You have made logical mistake .. Actually your code is true.

public boolean isConnected(){
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    if ( networkInfo != null && networkInfo.isConnectedOrConnecting()){
        return true;
    }else{
        return false;
    }
}



回答3:


ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null) {
    Toast.makeText(RefreshFunctionsActivity.this, "No save wifi detected.", Toast.LENGTH_LONG).show();
    return;
}



回答4:


Finally I used below method

public static boolean isNetworkConnected(Context c) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}


来源:https://stackoverflow.com/questions/31654999/boolean-android-net-networkinfo-isconnectedorconnecting-is-not-working-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!