Nullpointerexception getSystemService

use*_*494 5 android

如果我连接到WLAN,以下行将不会导致nullpointer异常:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Run Code Online (Sandbox Code Playgroud)

否则我会直接得到一个nullpointer-exception?我怎样才能解决这个问题?我希望我的后台服务只有在与wlan连接时才能工作.但是Nullpointerexception只会杀死整个应用......

Mac*_*rse 2

将以下行添加到清单中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Run Code Online (Sandbox Code Playgroud)