检查无线网络可用性时例外

时间:2014-05-26 10:34:54

标签: android android-wifi network-state

如果wifi连接可用,在asyncTask之前调用,并且在我的设备检测到nfc标签之后,我有这个代码用于chech:

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 个答案:

答案 0 :(得分:3)

检查你的上下文它似乎是null。从logcat也看起来像这样....一旦验证上下文:)

答案 1 :(得分:2)

contextnull。请检查您传递给connectionAvailable的值。如果您使用Activity作为Context,它应该正在运行。因此,您可以使用应用程序上下文。

答案 2 :(得分:1)

试试这个

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;
 }