我想检查手机上的互联网连接,以便播放音频。为了检查互联网连接,我编写了代码,它在除jellybean Nexus 7 平板电脑之外的所有 android 版本上都能正常工作。它在nexus 7上显示力量关闭。
这是我的检查代码。
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;
}
}
这是logcat 错误:
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)