本文介绍了boolean android.net.NetworkInfo.isConnectedOrConnecting()在Android 5.1.1中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Android Tablet应用程序,正在检查名为主页"的活动中的Internet连接.我已经在Android 4.2(平板电脑),4.4(平板电脑),5.0(手机),5.0.2(手机)中测试了该应用程序,该应用程序运行正常.
I am working on an Android Tablet Application, I am checking Internet connectivity within an Activity named "Home". I have tested the app in Android version 4.2 (Tablet), 4.4 (Tablet), 5.0 (Phone), 5.0.2 (Phone), the app is working fine.
问题:当我在Android 5.1.1(平板电脑)中测试该应用程序时,该应用程序被强制关闭.在日志中显示以下错误.
Problem:When I tested the app in Android 5.1.1 (Tablet), app is being forced close. Its showing following error in log.
日志:
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freestyle/com.freestyle.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread.access$800(ActivityThread.java:151)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.os.Handler.dispatchMessage(Handler.java:102)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.os.Looper.loop(Looper.java:135)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread.main(ActivityThread.java:5254)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at java.lang.reflect.Method.invoke(Native Method)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at java.lang.reflect.Method.invoke(Method.java:372)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at com.freestyle.utils.NetworkHelper.isConnectingToInternet(NetworkHelper.java:20)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at com.freestyle.Home.onCreate(Home.java:332)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.Activity.performCreate(Activity.java:5990)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
2015-07-27 05:36:19.312 ERROR: AndroidRuntime : ... 10 more
2015-07-27 05:36:19.313 WARN: ActivityManager : Force finishing activity 1 com.freestyle/.Home
2015-07-27 05:36:19.367 INFO: OpenGLRenderer : Initialized EGL, version 1.4
2015-07-27 05:36:19.369 DEBUG: mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.383 DEBUG: mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.817 WARN: ActivityManager : Activity pause timeout for ActivityRecord{2c5a4722 u0 com.freestyle/.Home t22999 f}
检查Internet连接的方法:
public boolean isConnectingToInternet() {
boolean status = false;
ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
//have to assign true
status = true;
return status;
}
return status;
}
家庭活动代码:
public class Home extends Activity implements OnClickListener {
NetworkHelper nh = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
nh = new NetworkHelper(HomeActivity.this);
if (nh.isConnectingToInternet()) {
ApplicationUpdates app = new ApplicationUpdates(HomeActivity.this,
loadingString, updateMessage);
app.fetchUpdate();
}
}
}
推荐答案
最后我用下面的方法
public static boolean isNetworkConnected(Context c) {
ConnectivityManager connectivityManager =
(ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
这篇关于boolean android.net.NetworkInfo.isConnectedOrConnecting()在Android 5.1.1中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!