case 7:
final Activity ctx = null;
final ConnectivityManager connectManager =
(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean connected =
(connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI ).getState() == NetworkInfo.State.CONNECTED );
Intent intent7 = new Intent(v.getContext(),News.class);
if(connected ){
startActivity(intent7);
}else{
Toast.makeText(Home.this, "No internet connection " , Toast.LENGTH_SHORT).show();
}
break;
它带有以下logcat
08-23 18:10:39.985: ERROR/AndroidRuntime(6555): Uncaught handler: thread main exiting due to uncaught exception
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): java.lang.NullPointerException
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at Test.bed.Home$1.onItemClick(Home.java:73)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.widget.AbsListView.onKeyUp(AbsListView.java:1757)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.widget.GridView.commonKey(GridView.java:1470)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.widget.GridView.onKeyUp(GridView.java:1382)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.KeyEvent.dispatch(KeyEvent.java:1249)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.View.dispatchKeyEvent(View.java:3683)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:758)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1691)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1111)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.app.Activity.dispatchKeyEvent(Activity.java:2045)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1667)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2390)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2360)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.os.Looper.loop(Looper.java:123)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at android.app.ActivityThread.main(ActivityThread.java:4595)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您将ctx
设置为null
,然后将其调用。这是你的强制关闭(由NullPointerException
引起)。
final Activity ctx = null;
final ConnectivityManager connectManager =(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
ctx is null^
您的活动是一个上下文,您只需致电this.getSystemService
(然后根本不需要ctx
。
答案 1 :(得分:2)
你也可以试试这个:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
但是,你得到一个NullPointerException
,因为第一个变量为空。