我正在开发一个 Android 平板电脑应用程序,我正在检查名为“Home”的 Activity 中的 Internet 连接。我已经在 Android 4.2(平板电脑)、4.4(平板电脑)、5.0(手机)、5.0.2(手机)版本中测试了该应用程序,该应用程序运行良好。
问题: 当我在 Android 5.1.1 (Tablet) 中测试应用程序时,应用程序被强制关闭。它在日志中显示以下错误。
日志:
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}
检查互联网连接的方法:
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();
}
}
}