getSystemService(Context.CONNECTIVITY_SERVICE)在移动数据上抛出异常

时间:2013-11-30 16:27:51

标签: android exception error-handling

我有一个实用程序方法,用于检查wifi连接并返回SSID。当移动设备在Wifi连接上时它可以正常工作,但是当我在移动数据连接上时它会失败。即使在实用程序中使用try catch块也不会捕获异常。使用调试日志验证getSystemService行没有执行任何操作。确切的代码是:

public static String getCurrentSsid(Context context) {

    String ssid = null;
    NetworkInfo networkInfo;
    ConnectivityManager connManager;

    try {
        connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        networkInfo = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    } catch (Exception e) {
        return null;
    }

    if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null) {
            ssid = connectionInfo.getSSID();
        }
    }
    return ssid;
}

logcat仅显示我正在调用此实用程序的Async任务的错误。知道我哪里错了吗?

11-30 21:42:38.723: W/dalvikvm(21207): threadid=14: thread exiting with uncaught exception (group=0x4165e2a0)
11-30 21:42:38.733: E/AndroidRuntime(21207): FATAL EXCEPTION: AsyncTask #1
11-30 21:42:38.733: E/AndroidRuntime(21207): java.lang.RuntimeException: An error occured while executing doInBackground()
11-30 21:42:38.733: E/AndroidRuntime(21207):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.lang.Thread.run(Thread.java:856)
11-30 21:42:38.733: E/AndroidRuntime(21207): Caused by: java.lang.NullPointerException
11-30 21:42:38.733: E/AndroidRuntime(21207):    at com.example.slapp.PhoneContactsListFragment$setInviteVisibilityAsync.doInBackground(PhoneContactsListFragment.java:1069)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at com.example.slapp.PhoneContactsListFragment$setInviteVisibilityAsync.doInBackground(PhoneContactsListFragment.java:1)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-30 21:42:38.733: E/AndroidRuntime(21207):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-30 21:42:38.733: E/AndroidRuntime(21207):    ... 5 more

1 个答案:

答案 0 :(得分:0)

发布的代码没有任何问题。

问题是,如果移动设备处于移动数据模式,该函数会将空值返回到我的异步任务。当我在那里处理返回值时,我没有检查空条件。所以我所要做的就是在实际检查之前添加一个空检查,如下所示:

String ssid = getCurrentSsid(Context);
if (ssid != null && ssid.equals("xyz")) { //do something}