-1

I have an app that works fine on a android phone , but when I try to run it on the Nexus7 which has no phone the code fails with a force stop at the location indicated. What is the solution? How do I check to see if the feature is there and what should I do to solve this?

  ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
  boolean isWifiConn = networkInfo.isConnected();
  printi("oopsA",6);
  networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  printi("oopsB",6);
  boolean isMobileConn = networkInfo.isConnected();  //<<<<FAILS HERE ON NEXUS 7
user1445716
  • 442
  • 6
  • 18

4 Answers4

4

Your networkInfo is probably null. You have to test that before. This means you can't access to this type of connectivityManager.

Try this:

networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

boolean isMobileConn = false;
if(networkInfo != null)
    isMobileConn = networkInfo.isConnected();  
AMerle
  • 4,354
  • 1
  • 28
  • 43
2

I have faced same problem with Motorola Xoom, because it does not have connectivity support for ConnectivityManager.TYPE_MOBILE.

Following code is working fine for me :

ConnectivityManager connMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
    return connMngr.getActiveNetworkInfo().isConnected();
}
catch (NullPointerException npe) {
    return false;
}
Frederic
  • 3,274
  • 1
  • 21
  • 37
Manish Lomte
  • 329
  • 4
  • 17
0

Check permissions in AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
0

Corrected Code as follows:

IsAPhone=0;
try{
    boolean isMobileConn = false;
    if(networkInfo != null){ isMobileConn = networkInfo.isConnected();IsAPhone=1;}  
   }
catch (Exception e) {}
user1445716
  • 442
  • 6
  • 18