开发者

checking Internet before opening intent

please explain me what's wrong with following code. I am trying to detect if there is an Internet connection before launching an intent and otherwise display a toast stating that there is no Internet connection. It's coming with force to close .

 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;  

Its coming with the following 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 1开发者_如何学Go8: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)


You set ctx to null and then calling it. This is your Force Close( caused by NullPointerException).

final Activity ctx = null;
final ConnectivityManager connectManager =(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
                                                      ctx is null^

Your activity is a context you can just call this.getSystemService (and then there no need for ctx at all.


You could also try this:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

But yeah, you're getting a NullPointerException because the first variable is null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜