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;
}
}