Context.getSystemService() 在空对象引用上
Context.getSystemService() on a null object reference
每当我尝试在我的应用程序中登录时,此 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.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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;
}
}
我猜你忘了在清单中声明应用程序。
您必须添加
android:name=".AIOUSecurity"
进入 <application>
标签
清单
您的问题是上下文为空(可能应用未初始化或者您丢失了引用,我不确定这一点)。
如果我理解您的意图,AppConfig 似乎是一个带有一些静态方法的实用程序 class。
对于您的用例,为什么不将上下文作为参数传递给方法,这样您就可以确定您拥有该上下文?
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);
}
每当我尝试在我的应用程序中登录时,此 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.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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;
}
}
我猜你忘了在清单中声明应用程序。
您必须添加
android:name=".AIOUSecurity"
进入 <application>
标签
清单
您的问题是上下文为空(可能应用未初始化或者您丢失了引用,我不确定这一点)。
如果我理解您的意图,AppConfig 似乎是一个带有一些静态方法的实用程序 class。 对于您的用例,为什么不将上下文作为参数传递给方法,这样您就可以确定您拥有该上下文?
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);
}