Question

I have a utility class in my Android project. Whenever I call one of its helper methods, though, the app crashes. For example, when I call this:

public boolean IsNetworkAvailable(Context context){
    ConnectivityManager connectivity = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Toast.makeText(context, "None Available", Toast.LENGTH_SHORT).show();
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        for(NetworkInfo inf : info){
            if(inf.getTypeName().contains("WIFI"))
                if(inf.isConnected())
                    return true;
        }
    }
    return false;
}

...from MainActivity like so:

SteinbeckCityUtils SteinbeckCityUtils = new SteinbeckCityUtils();
boolean networkAvailable = SteinbeckCityUtils.IsNetworkAvailable(MainActivity.this);
if (networkAvailable) {
    Toast tostito = Toast.makeText(MainActivity.this, "Network is available", Toast.LENGTH_SHORT);
    tostito.setGravity(Gravity.CENTER, 0, 0);
    tostito.show();
}

Even when I changed the middle of IsNetworkAvailable() to this it fails:

if (null == connectivity) {
        //Toast.makeText(context, "None Available", Toast.LENGTH_SHORT).show();
        Log.i("czechNetwork", "no signs of life");
    } else {
        Log.i("czechNetwork", "some sign of life");

Here is more code from the class for more context:

package hhs.app;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class HHSUtils {
    private Context _context;

    public void ConnectionDetector(Context context) {
        this._context = context;
    }

    // Found this here: http://www.androidsnippets.com/enable-and-disable-wifi
   public void enableWiFi(boolean enable) {
       WifiManager wifi = (WifiManager) _context.getSystemService(Context.WIFI_SERVICE);
       wifi.setWifiEnabled(enable);
   }

    // Found this here: http://www.androidsnippets.com/checking-for-wifi-access
    public boolean IsNetworkAvailable(Context context){
        ConnectivityManager connectivity = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            Toast.makeText(context, "None Available", Toast.LENGTH_SHORT).show();
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            for(NetworkInfo inf : info){
                if(inf.getTypeName().contains("WIFI"))
                    if(inf.isConnected())
                        return true;
            }
        }
        return false;
    }
}

I tried just calling the method directly without instantiating the class, too (with HHS.IsNetworkAvailable()), but doing it that way, the method is not even recognized...

Était-ce utile?

La solution

You set the permissions in AndroidManifest.xml?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top