I try to check the status of my network (connected or disconnected) using this function:
// Check Network status
private fun isNetworkAvailable(): Boolean {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE)
return if (connectivityManager is ConnectivityManager) {
val networkInfo = connectivityManager.activeNetworkInfo
networkInfo.isConnected
}
else false
}
This gives me a java.lang.IllegalStateException: networkInfo must not be null - error when run with a disconnected network. Why? And how can I solve this?