I want to check internet connection on my phone so i can play audio. to check internet connection i wrote code and it works fine on all android version except jellybean Nexus 7 tablet . it show force close on nexus 7.
Here is my code for checking.
private void MyCheckinternet() {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
else
{
connected = false;
}
}
And this is logcat error:
09-18 16:20:33.098: E/AndroidRuntime(3224): FATAL EXCEPTION: main
09-18 16:20:33.098: E/AndroidRuntime(3224): java.lang.NullPointerException
09-18 16:20:33.098: E/AndroidRuntime(3224):at
com.android.twitter.Main.MyCheckinternet(Vmix_Main.java:115)
09-18 16:20:33.098:E/AndroidRuntime(3224): at com.android.twitter.Main.access$0(Main.java:112)
09-18 16:20:33.098: E/AndroidRuntime(3224): at
com.android.twitter.Main$3.onClick(Main.java:520)
09-18 16:20:33.098: E/AndroidRuntime(3224): at
android.view.View.performClick(View.java:4084)
09-18 16:20:33.098: E/AndroidRuntime(3224): at
android.view.View$PerformClick.run(View.java:16966)
09-18 16:20:33.098: E/AndroidRuntime(3224): at
android.os.Handler.handleCallback(Handler.java:615)
09-18 16:20:33.098: E/AndroidRuntime(3224): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-18 16:20:33.098: E/AndroidRuntime(3224): at android.os.Looper.loop(Looper.java:137)
connectivityManager
or
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
is null
My bet is on the second one. test it for null before testing the state of it. (the networkInfo may be null if the mobile network is not activated)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.