nullpointer exception on connectivitymanager
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();}
I'm getting a开发者_JAVA百科 nullpointer on return cm.getActiveNetworkInfo
.
I tried this.getSystemService
but also didn't work. Maybe its because my activity extends a super activity i've created and I'm grabbing the context incorrectly...? tried getBaseContext()
too
With two methods chained together on the return line, are you sure it's the ConnectivityManager
coming back null? It's perfectly valid for ConnectivityManager.getActiveNetworkInfo()
to return null
instead of a valid NetworkInfo
object when there is no active network. If this comes back null
, then it is the call to NetworkInfo.isConnectedOrConnecting()
that is throwing the exception.
Guess #1:
Do you have these in your manifest?
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE">
精彩评论