开发者

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">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜