我使用檢查互聯網連接下面的方法,但它崩潰..檢查網絡連接不工作
public static boolean CheckInternetConnection() {
ConnectivityManager connec = (ConnectivityManager) PropertyApplication
.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi != null && wifi.isConnected()) {
return true;
} else if (mobile != null && mobile.isConnected()) {
return true;
}
return false;
}
此外,我已經添加使用許可清單文件。這裏是我的清單
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
,這裏是我的logcat
02-23 09:43:37.549: D/dalvikvm(1896): GC_FOR_ALLOC freed 36K, 8% free 2867K/3108K, paused 7ms, total 8ms
02-23 09:43:37.573: I/dalvikvm-heap(1896): Grow heap (frag case) to 3.909MB for 960012-byte allocation
02-23 09:43:37.673: D/dalvikvm(1896): GC_FOR_ALLOC freed 2K, 7% free 3802K/4048K, paused 49ms, total 49ms
02-23 09:43:37.729: D/main(1896): inside main
02-23 09:43:37.837: D/libEGL(1896): loaded /system/lib/egl/libEGL_genymotion.so
02-23 09:43:37.849: D/(1896): HostConnection::get() New Host Connection established 0xb93084a8, tid 1896
02-23 09:43:37.877: D/libEGL(1896): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-23 09:43:37.901: D/libEGL(1896): loaded /system/lib/egl/libGLESv2_genymotion.so
02-23 09:43:37.997: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented
02-23 09:43:37.997: E/OpenGLRenderer(1896): Getting MAX_TEXTURE_SIZE from GradienCache
02-23 09:43:38.017: E/OpenGLRenderer(1896): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-23 09:43:38.017: D/OpenGLRenderer(1896): Enabling debug mode 0
02-23 09:43:39.745: D/main(1896): inside finally
02-23 09:43:40.045: D/dalvikvm(1896): GC_FOR_ALLOC freed 248K, 11% free 3750K/4200K, paused 8ms, total 8ms
02-23 09:43:40.069: I/dalvikvm-heap(1896): Grow heap (frag case) to 4.931MB for 1127532-byte allocation
02-23 09:43:40.097: D/dalvikvm(1896): GC_FOR_ALLOC freed 1K, 9% free 4850K/5304K, paused 23ms, total 23ms
02-23 09:43:40.257: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented
02-23 09:43:42.513: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented
02-23 09:43:49.373: D/dalvikvm(1896): GC_FOR_ALLOC freed 243K, 8% free 5201K/5644K, paused 54ms, total 65ms
02-23 09:43:49.577: I/dalvikvm-heap(1896): Grow heap (frag case) to 6.237MB for 1011060-byte allocation
02-23 09:43:49.653: D/dalvikvm(1896): GC_FOR_ALLOC freed 987K, 22% free 5200K/6632K, paused 71ms, total 71ms
02-23 09:43:50.077: D/dalvikvm(1896): GC_FOR_ALLOC freed 11K, 22% free 5189K/6632K, paused 74ms, total 77ms
02-23 09:43:50.709: I/dalvikvm-heap(1896): Grow heap (frag case) to 9.118MB for 4044204-byte allocation
02-23 09:43:50.789: D/dalvikvm(1896): GC_FOR_ALLOC freed <1K, 14% free 9138K/10584K, paused 78ms, total 78ms
02-23 09:43:51.089: D/main(1896): inside prop home onCreate
02-23 09:43:51.129: I/Choreographer(1896): Skipped 148 frames! The application may be doing too much work on its main thread.
02-23 09:43:51.285: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented
02-23 09:43:53.609: D/main(1896): inside prop home intent
02-23 09:43:53.989: D/main(1896): inside prop home starting intent
02-23 09:43:54.101: D/main(1896): inside prop list onCreate
02-23 09:43:54.101: D/main(1896): inside prop list try block
02-23 09:43:54.109: D/main(1896): inside prop list try blocktrue
02-23 09:43:54.117: W/System.err(1896): java.lang.NullPointerException
02-23 09:43:54.121: W/System.err(1896): at com.propvis.utils.PropertyUtil.CheckInternetConnection(PropertyUtil.java:61)
02-23 09:43:54.137: W/System.err(1896): at com.propvis.activity.PropertyListActivity.onCreate(PropertyListActivity.java:41)
02-23 09:43:54.149: W/System.err(1896): at android.app.Activity.performCreate(Activity.java:5133)
02-23 09:43:54.149: W/System.err(1896): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-23 09:43:54.165: W/System.err(1896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-23 09:43:54.165: W/System.err(1896): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-23 09:43:54.169: W/System.err(1896): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-23 09:43:54.185: W/System.err(1896): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-23 09:43:54.189: W/System.err(1896): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 09:43:54.201: W/System.err(1896): at android.os.Looper.loop(Looper.java:137)
02-23 09:43:54.205: W/System.err(1896): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-23 09:43:54.217: W/System.err(1896): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 09:43:54.217: W/System.err(1896): at java.lang.reflect.Method.invoke(Method.java:525)
02-23 09:43:54.241: W/System.err(1896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-23 09:43:54.245: W/System.err(1896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 09:43:54.253: W/System.err(1896): at dalvik.system.NativeStart.main(Native Method)
02-23 09:43:54.421: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented
很抱歉,但,怎麼能不返回一個值?它要麼返回true,false,要麼崩潰? – Merlevede
我想這是崩潰.. –
崩潰與什麼異常? – EJP