简体   繁体   中英

Application crashes when checking if Internet is online

I'm trying to recognize if the connection from the user is offline or online.

I built a class to check it:

public class wifi {
    public static Context context;
    public static ConnectivityManager connec;
    public static final boolean isInternetOn(){

        // get Connectivity Manager object to check connection
        connec =  (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);

           // Check for network connections
            if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                 connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                 connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                 connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

                // if connected with internet

                Toast.makeText(context, " Connected ", Toast.LENGTH_LONG).show();
                return true;

            } else if ( 
              connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
              connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {

                Toast.makeText(context, " Not Connected ", Toast.LENGTH_LONG).show();
                return false;
            }
          return false;
        }
}

And call it when I need,

My Main Activity:

public void login(View view) {
    if(wifi.isInternetOn() == true){
    gpsTracker = new GPSTracker(this);
    if (gpsTracker.getIsGPSTrackingEnabled())
    {
    Latitude = String.valueOf(gpsTracker.latitude);
    Longitude = String.valueOf(gpsTracker.longitude);

    Session.slatitude = Latitude;
    Session.slongitude = Longitude;

    String getemail = email.getText().toString();
    String getpassword = password.getText().toString();
    new SigninActivity(this, status).execute(getemail, getpassword,Latitude,Longitude);
    }
    }
}

But when I run my code I receive these errors:

07-02 15:34:24.052: E/AndroidRuntime(10026): FATAL EXCEPTION: main 07-02 15:34:24.052: E/AndroidRuntime(10026): 
java.lang.ExceptionInInitializerError 07-02 15:34:24.052: E/AndroidRuntime(10026):  
at com.tapp.myapp.MainActivity.login(MainActivity.java:168) 07-02 15:34:24.052: 
E/AndroidRuntime(10026):    at com.tapp.myapp.MainActivity$1$1.onCompleted(MainActivity.java:116) 07-02 15:34:24.052: 
E/AndroidRuntime(10026): Caused by: java.lang.NullPointerException 07-02 15:34:24.052: 
E/AndroidRuntime(10026):    at com.tapp.myapp.wifi.<clinit>(wifi.java:9) 07-02 15:34:24.052: E/AndroidRuntime(10026):   ... 13 more

What am I doing wrong?

Check your Context value. Context is null .

ConnectivityManager#getNetworkInfo(int type) will return null if the type you pass in is not supported by the device.

I'm not sure why you are passing constant values of 0 and 1 into this method, but my guess is that those parameters don't mean what you think they mean.

You should be using ConnectivityManager 's TYPE_ constants such as TYPE_MOBILE .

The constants you are using (0 and 1) refer to TYPE_MOBILE and TYPE_WIFI respectively, but if you are running this on a device such as an emulator, one or both of this could easily be unsupported.

Because a device may not support one or more of the types you are checking, you should also be null-checking the return value from getNetworkInfo() .

Note that if all you care about is whether the user is connected to the internet, you probably don't need to check explicit types anyway. Just call getActivityNetworkInfo() and you will get the user's active network connection. Check if it is null (they may not have an active connection), then check its state to see if it is connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM