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.
Problem: When I tested the app in Android 5.1.1 (Tablet), app is being forced close. Its showing following error in log.
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}
Method to check Internet Connectivity:
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;
}
Code of Home Activity:
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();
}
}
}
check this way. Its work fine for mw
public static boolean checkInternetConnection(Context context)
{
try
{
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}