什么是WiFi broadCastReceiver的wron?

时间:2014-11-07 16:47:19

标签: android broadcastreceiver android-wifi

我刚刚创建了一个应用,以了解ConnectivitManagerbroadCastReceiver的工作原理。但是,当我运行应用程序时,它崩溃,我不知道错误来自哪里。租约已经在Logcat输出。

代码

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-07 17:39:37.309: E/AndroidRuntime(27577): FATAL EXCEPTION: main
11-07 17:39:37.309: E/AndroidRuntime(27577): Process: com.example.wifi_socket_01, PID: 27577
11-07 17:39:37.309: E/AndroidRuntime(27577): 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@430336e8
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at 
android.os.Handler.handleCallback(Handler.java:733)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
android.os.Handler.dispatchMessage(Handler.java:95)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at android.os.Looper.loop(Looper.java:157)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at   
android.app.ActivityThread.main(ActivityThread.java:5293)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at java.lang.reflect.Method.invokeNative(Native 
Method)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
java.lang.reflect.Method.invoke(Method.java:515)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at dalvik.system.NativeStart.main(Native Method)
11-07 17:39:37.309: E/AndroidRuntime(27577): Caused by: java.lang.NullPointerException
11-07 17:39:37.309: E/AndroidRuntime(27577):    at    
com.example.wifi_socket_01.MainActivity$1.onReceive(MainActivity.java:51)
11-07 17:39:37.309: E/AndroidRuntime(27577):    at  
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)

1 个答案:

答案 0 :(得分:0)

这似乎是罪魁祸首:

11-07 17:39:37.309: E/AndroidRuntime(27577): Caused by: java.lang.NullPointerException
11-07 17:39:37.309: E/AndroidRuntime(27577):    at    
com.example.wifi_socket_01.MainActivity$1.onReceive(MainActivity.java:51)

第51行的内容是什么?也许这就是问题所在:

NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);

您是否尝试过显式转换为NetworkInfo对象?

NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);