-1

I'm really new on android and I'm working on an app that needs to check if the device is connected to the internet. This is the error that I keep getting.

java.lang.IllegalStateException: System services not available to Activities before onCreate() at android.app.Activity.getSystemService

public class Fragment_Home extends Fragment implements Class_Home_Adapter.OnItemClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
    readJsonFile();
return v;
}

private void readJsonFile() { 
        Class_Check_Internet_Connection class_check_internet_connection = new Class_Check_Internet_Connection();
        Log.e(TAG, "Internet Status: " + class_check_internet_connection.internetStatus());
}

This is the class that I'm using to check the internet connection

public class Class_Check_Internet_Connection extends Activity {
    public static boolean isNetworkAvailable(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if ((activeNetworkInfo != null)&&(activeNetworkInfo.isConnected())){
            return true;
        }else{
            return false;
        }
    }
    public boolean internetStatus() {
        boolean isNetworkAvailable = Class_Check_Internet_Connection.isNetworkAvailable(this);
        return isNetworkAvailable;
    }
}
ADM
  • 20,406
  • 11
  • 52
  • 83
Carlos Castro
  • 132
  • 1
  • 11
  • Possible duplicate of ["System services not available to Activities before onCreate()" Error message?](https://stackoverflow.com/questions/11645906/system-services-not-available-to-activities-before-oncreate-error-message) – ADM Dec 28 '18 at 17:02
  • There is no reason to "extend Activity" in your `Class_Check_Internet_Connection ` class. Because you are extending `Activity` the class expects the `onCreate()` method, where the `Context` would be created. But you are "bypassing" the creation of a `Context` ...so `this` is null! – Barns Dec 28 '18 at 17:28

1 Answers1

1

There is no reason to "extend Activity" in your Class_Check_Internet_Connection class. It appears you are extending Activity in your class--probably trying to force the creation of a Context. The Context is created in the onCreate() method. But you are "bypassing" the creation of a Context because there is no onCreate()method ...so this is null!

But you do not need to create a Context--nor should you. You should not extend Activity for this class! Instead pass the Context as a parameter to the class.

// Snake case is generally not used with Jave, but I will leave it...
public class Class_Check_Internet_Connection {

    Context mContext = null;
    public Class_Check_Internet_Connection(Context context){
        this.mContext = context;
    }

    public static boolean isNetworkAvailable(){
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if ((activeNetworkInfo != null)&&(activeNetworkInfo.isConnected())){
            return true;
        }else{
            return false;
        }
    }

    // another public (!) method to simply query isNetworkAvailable?? If you think you need it....
    public boolean internetStatus() {
        boolean isNetworkAvailable = Class_Check_Internet_Connection.isNetworkAvailable();
        return isNetworkAvailable;
    }
}

// Strange name for a method that checks the internet connectivity!!
private void readJsonFile() { 
        Class_Check_Internet_Connection class_check_internet_connection = new Class_Check_Internet_Connection(this);
        Log.e(TAG, "Internet Status: " + class_check_internet_connection.internetStatus());
}




Note:
I did not check your code to see if it operates properly. I am just addressing the error that you received.

Barns
  • 4,850
  • 3
  • 17
  • 31