As the title says, I can't get State in Broadcastreceiver from NetWorkInfo Class.
It returns some errors in log output which are given below.
I need your help.
Here is the log output and the Broacastreceiver class , I also notice that I've added all permission (Access Network State et Internet ) and the Broadcast intent action android:name="android.net.conn.CONNECTIVITY_CHANGE
.
So I dare say there isn't problem in manifest.xml file .
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
State state = activeNetwork.getState();
switch (state) {
case CONNECTING:
Toast.makeText(context, "connecting", Toast.LENGTH_SHORT).show();
break;
and the log output is:
java.lang.RuntimeException: Unable to start receiver com.example.test.ConnectivityChangeReceiver: java.lang.NullPointerException
E/AndroidRuntime( 9873): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2668)
E/AndroidRuntime( 9873): at android.app.ActivityThread.access$1800(ActivityThread.java:172)
E/AndroidRuntime( 9873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
E/AndroidRuntime( 9873): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 9873): at android.os.Looper.loop(Looper.java:146)
E/AndroidRuntime( 9873): at android.app.ActivityThread.main(ActivityThread.java:5653)
E/AndroidRuntime( 9873): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9873): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 9873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
E/AndroidRuntime( 9873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
E/AndroidRuntime( 9873): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 9873): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9873): at com.example.test.ConnectivityChangeReceiver.onReceive(ConnectivityChangeReceiver.java:27)
E/AndroidRuntime( 9873): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2653)