ConnectivityManager上的空指针异常

时间:2013-06-05 10:56:50

标签: android nullpointerexception connectivity

我的aplication类是获取aplication上下文的活动,并使用其他where.isOnline从某个类JSONParser调用,这不是一个活动。 isOnline由getJSONFromUrl()调用。 Test是B class.test()的方法,由活动LoginActivity调用。但是,我得到nullpoint,不明白为什么。 是因为我以错误的方式打印上下文,或者我将上下文传递给ConnectivityManager incorecly或其他什么? 方法isOnline

private static Boolean isOnline(){

        context = MyApplication.getAppContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
         if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
               for (int i = 0; i < info.length; i++) {
                  if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                     return true;
                  }
               }
            }
         }
         return false;
    }

myaplication class

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

错误

06-05 09:29:22.104: D/dalvikvm(526): GC_EXTERNAL_ALLOC freed 1033 objects / 82800 bytes in 52ms
06-05 09:29:52.734: D/frakc@mail.ru(526): emails
06-05 09:29:56.414: D/f(526): onItemSelected

06-05 09:30:00.446: W/KeyCharacterMap(526): No keyboard for id 0
06-05 09:30:00.446: W/KeyCharacterMap(526): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-05 09:30:01.814: D/parsing data to login(526): true
06-05 09:30:01.824: W/dalvikvm(526): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
06-05 09:30:01.824: E/AndroidRuntime(526): FATAL EXCEPTION: Thread-10
06-05 09:30:01.824: E/AndroidRuntime(526): java.lang.NullPointerException
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.adapter.JSONParser.isOnline(JSONParser.java:87)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.adapter.JSONParser.getJSONFromUrl(JSONParser.java:114)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.UserFunctions.loginUser(UserFunctions.java:51)
06-05 09:30:01.824: E/AndroidRuntime(526):  at ua.mirkvartir.android.frontend.LoginActivity$1$1.run(LoginActivity.java:144)
06-05 09:30:01.824: E/AndroidRuntime(526):  at java.lang.Thread.run(Thread.java:1096)

3 个答案:

答案 0 :(得分:0)

变化:

MyApplication.context = getApplicationContext();

MyApplication.context = this;

答案 1 :(得分:0)

如果你还没有做,请做以下事情:

1。在您的应用程序名称中添加您的Manifest中的应用程序类

 <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:name=".MyApplication"

2。添加ACCESS_NETWORK_STATE权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

答案 2 :(得分:0)

你的上下文无效。传递方法中的应用程序上下文,然后使用该上下文进行引用..它将正常工作