关于null对象引用的Context.getSystemService()

时间:2018-03-22 07:53:36

标签: java android android-networking

每当我尝试在我的应用程序中登录时, getSystemService() 方法都会产生NPE。请。帮助,因为现有的解决方案不能回答我的问题。我已经打电话给 super.oncreate()

堆栈跟踪

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at com.example.aiousecurityapplication.Utill.AppConfig.isNetworkAvailable(AppConfig.java:19)
at com.example.aiousecurityapplication.Utill.JSONSenderReceiver.makeHttpRequest(JSONSenderReceiver.java:40)
at com.example.aiousecurityapplication.Activities.SigninActivity$login.doInBackground(SigninActivity.java:200)
at com.example.aiousecurityapplication.Activities.SigninActivity$login.doInBackground(SigninActivity.java:151)
at android.os.AsyncTask$2.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
at java.lang.Thread.run(Thread.java:776) 

方法位置

public class AppConfig {
// Server  url
public static String URL_MAIN = "http://10.14.36.4/aiousecurity/index.php";
public static final String FROM_CNIC = "fromCnic";
public static final String ID = "id";

public static boolean isNetworkAvailable() {

    boolean flag = false;
    ConnectivityManager connMgr =
            (ConnectivityManager) AIOUSecurity.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connMgr!= null) {
        NetworkInfo networkInfo= connMgr.getActiveNetworkInfo();
        flag = (networkInfo != null && networkInfo.isConnected());
    }
        return (flag);
}

}

我还打电话给 super.oncreate(); 请。参考下面的代码

public class AIOUSecurity extends Application {
private static Context context;

public static int screenHeight;
public static int screenWidth;

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();

    DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    screenHeight = displayMetrics.heightPixels;
    screenWidth = displayMetrics.widthPixels;
}

public static Context getContext() {
    return context;
}

}

2 个答案:

答案 0 :(得分:1)

我猜你忘了在清单中声明申请。

你必须添加 android:name=".AIOUSecurity"标记为<application> 清单

答案 1 :(得分:0)

您的问题是上下文为空(可能是应用程序未初始化或您丢失了引用,我不确定这一点)。

如果我理解你要做什么,AppConfig看起来像是一个带有一些静态方法的实用程序类。 对于您的用例,为什么不将上下文作为参数传递给方法,以便您可以确定您具有该上下文?

public class AppConfig {
// Server  url
public static String URL_MAIN = "http://10.14.36.4/aiousecurity/index.php";
public static final String FROM_CNIC = "fromCnic";
public static final String ID = "id";

public static boolean isNetworkAvailable(Context context) {

    boolean flag = false;
    ConnectivityManager connMgr =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connMgr!= null) {
        NetworkInfo networkInfo= connMgr.getActiveNetworkInfo();
        flag = (networkInfo != null && networkInfo.isConnected());
    }
    return (flag);
}