在写一个广播包来监听网络状态改变时,在解锁手机时出现java.lang.NullPointerException的错误,错误显示为net_status这个TextView控件变成了空指针,但是不知道为什么变成了空指针,自己在onCreate函数里有通过net_status = (TextView) findViewById(R.id.connect);获得该控件了。
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
if (connectMgr != null) {
NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mobNetInfo != null && wifiNetInfo != null) {
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
net_status.setText("NetWork Disabled");
} else {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
if (connectMgr != null) {
NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mobNetInfo != null && wifiNetInfo != null) {
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
net_status.setText("NetWork Disabled");
} else {