开发者

nullpointer exception on connectivitymanager

开发者 https://www.devze.com 2023-03-16 10:00 出处:网络
public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
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

精彩评论

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