尝试使用ConnectivityManager.CONNECTIVITY_ACTION测试broadCastReceiver时,应用程序崩溃,并且logcat显示Error receiving broadcast Intent。请告诉我我错过了什么。
代码:
代码:
public class wifi_socket_01 extends Activity {
TextView tv_conn_status;
TextView tv_conn_ssid;
TextView tv_conn_speed;
TextView tv_conn_strengt;
TextView tv_conn_ip;
EditText et_ip;
ToggleButton tobBtn_connect;
EditText et_msg;
Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetUpAllViews(R.layout.activity_main);
displayWiFiStates();
this.registerReceiver(this.myWiFiReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver myWiFiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
displayWiFiStates();
}
}
};
private void SetUpAllViews(int layout) {
// TODO Auto-generated method stub
setContentView(layout);
tv_conn_status = (TextView) findViewById(R.id.tv_conn_status);
tv_conn_ssid = (TextView) findViewById(R.id.tv_conn_ssid);
tv_conn_speed = (TextView) findViewById(R.id.tv_conn_speed);
tv_conn_strengt = (TextView) findViewById(R.id.tv_conn_strength);
tv_conn_ip = (TextView) findViewById(R.id.tv_conn_ip);
et_ip = (EditText) findViewById(R.id.et_ip);
tobBtn_connect = (ToggleButton) findViewById(R.id.togbtn_connect);
et_msg = (EditText) findViewById(R.id.et_msg);
btn_send = (Button) findViewById(R.id.btn_send);
}
protected void displayWiFiStates() {
// TODO Auto-generated method stub
String service = Context.CONNECTIVITY_SERVICE;
ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(service);
NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWiFiInfo = myWifiManager.getConnectionInfo();
if(myNetworkInfo.isConnected()) {
tv_conn_ip.setText("IP: "+myWiFiInfo.getIpAddress());
}else {
tv_conn_status.setText("Disconnected");
}
}
}
LogCat:
11-08 10:26:57.645: E/AndroidRuntime(15348): FATAL EXCEPTION: main
11-08 10:26:57.645: E/AndroidRuntime(15348): Process: com.example.wifi_socket_01, PID:
15348
11-08 10:26:57.645: E/AndroidRuntime(15348): java.lang.RuntimeException: Error
receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010
(has extras) } in com.example.wifi_socket_01.MainActivity$1@4302f638
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
11-08 10:26:57.645: E/AndroidRuntime(15348): at a
ndroid.os.Handler.handleCallback(Handler.java:733)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.os.Handler.dispatchMessage(Handler.java:95)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.os.Looper.loop(Looper.java:157)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.ActivityThread.main(ActivityThread.java:5293)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
java.lang.reflect.Method.invokeNative(Native Method)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
java.lang.reflect.Method.invoke(Method.java:515)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
dalvik.system.NativeStart.main(Native Method)
11-08 10:26:57.645: E/AndroidRuntime(15348): Caused by: java.lang.NullPointerException
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.example.wifi_socket_01.MainActivity$1.onReceive(MainActivity.java:48)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)