我尝试使用以下功能来检查我的网络状态(已连接或未连接):
// 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
}
当网络断开时运行代码会报java.lang.IllegalStateException: networkInfo must not be null - error错误。为什么会出现这个错误?如何解决?