Search code examples
androidcrashtabletconnectivity

Android check network connectivity on some tablets crash


I know this topic has already been asked here, but I don't understand why my code is not working as expectd. I need to check data connectivity at launch of my app, but it crashes on some tablets (as Nexus 7), but not on all.

My code :

public static boolean getNetworkState(Context pContext)
{
    ConnectivityManager connect =  (ConnectivityManager)pContext.getSystemService(Context.CONNECTIVITY_SERVICE);

    if(connect != null)
    {
        if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
    else
        return false;
}

public static boolean getWifiState(Context context)
{
    ConnectivityManager connect =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if ( connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting())
    {
        return true;
    }
    else 
    {
        return false;
    }
}

Connectivity check :

if(Commons.getNetworkState(this) || Commons.getWifiState(this))
        {
            loadData();
        }
        else
        {
            Commons.getConnectivityErrorMessage(this);
        }

Normally, if network connectivity is not supported on tablet, the "connectivity" object in "getNetworkState" should be null but it seems it's not the case because it crashed on line

 if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())

What's wrong here? Permissions are already written

EDIT : Unfortunately, I don't have a tablet to test the problem. I just have user report on developer console. The following report has been reported by an user with a Nexus 7 :

java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.MeteocielActivity}: java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:178)
at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:523)
at android.app.ActivityGroup.onResume(ActivityGroup.java:61)
at com.meteociel.fr.activities.MeteocielActivity.onResume(MeteocielActivity.java:69)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
... 12 more
Caused by: java.lang.NullPointerException
at com.meteociel.fr.classes.Commons.getNetworkState(Commons.java:36)
at com.meteociel.fr.activities.HomeActivity.onResume(HomeActivity.java:98)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
... 19 more

EDIT 2 : all the permissions which I use :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Solution

  • This code works now :

    public static boolean getNetworkState(Context pContext)
        {
            ConnectivityManager connect = null;
            connect =  (ConnectivityManager)pContext.getSystemService(pContext.CONNECTIVITY_SERVICE);
    
            if(connect != null)
            {
                NetworkInfo result = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (result != null && result.isConnectedOrConnecting())
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            else
                return false;
        }