Исключение при проверке доступности Wi-Fi

У меня есть этот код для чеха, если Wi-Fi соединение доступно, вызывается перед asyncTask, и после того, как мое устройство обнаруживает тег nfc:

public static boolean connectionAvailable(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    Log.i("UTILS", connManager.toString());

    NetworkInfo mWifi = connManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    Log.i("UTILS", mWifi.toString());

    return mWifi.isConnected();
}

Теперь я получаю эту ошибку:

05-26 12:20:14.157: E/InputEventReceiver(19847): Exception dispatching input event.
05-26 12:20:14.157: E/MessageQueue-JNI(19847): Exception in MessageQueue callback: handleReceiveCallback
05-26 12:20:14.167: E/MessageQueue-JNI(19847): java.lang.NullPointerException
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.example.Utils.connectionAvailable(Utils.java:14)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.example.fragments.CustomerSummary.completeRequest(CustomerSummary.java:124)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.example.SignInActivity.onTouch(SignInActivity.java:232)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.View.dispatchTouchEvent(View.java:7701)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.View.dispatchPointerEvent(View.java:7886)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at  android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at  android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.os.MessageQueue.nativePollOnce(Native Method)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.os.MessageQueue.next(MessageQueue.java:138)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.os.Looper.loop(Looper.java:123)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at java.lang.reflect.Method.invoke(Method.java:515)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 12:20:14.167: E/MessageQueue-JNI(19847):  at dalvik.system.NativeStart.main(Native Method)

где строка 14 - это строка:

.getSystemService(Context.CONNECTIVITY_SERVICE);w

В чем дело?

3 ответа

Решение

Проверьте ваш контекст, это кажется нулевым. Из логката тоже вроде так.... Один раз проверь контекст:)

context является null, Пожалуйста, проверьте значение, которое вы передаете connectionAvailable, Если вы используете Activity как Context, это должно быть запущено. Таким образом, вы можете использовать контекст приложения вместо.

Попробуй это

public static boolean connectionAvailable(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

if (connManager != null) {
    Log.i("UTILS", connManager.toString());
        NetworkInfo mWifi = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi != null) {
        Log.i("UTILS", mWifi.toString());

        return mWifi.isConnected();
    }
   }
     return false;
 }
Другие вопросы по тегам