gpt4 book ai didi

android - 无法在我的监听器和广播接收器中涉及接口(interface)方法空对象引用

转载 作者:行者123 更新时间:2023-11-29 15:43:52 27 4
gpt4 key购买 nike

也许我的问题标题在 stackoverflow 上有一些其他问题,但我找不到任何有用的信息,所以我需要任何人帮助解决我的特定问题:我想连续检查互联网连接,所以我创建了一个广播接收器扩展类,它有如下监听器:

NetworkStateReceiver.java:

public class NetworkStateReceiver extends BroadcastReceiver {

public interface NetworkStateListener {
void onNetChange(int status);
}

private NetworkStateListener mOnNetworkStateListener;

public void setNetworkStateListener(NetworkStateListener listener) {
mOnNetworkStateListener = listener;
}

public void onReceive(Context context, Intent intent) {
int status = NetworkUtil.getConnectivityStatus(context);
if (mOnNetworkStateListener != null)
mOnNetworkStateListener.onNetChange(status);
}
}

在我的 Activity 中:

public class FrgChoosing extends Fragment {
private NetworkStateReceiver mNetworkStateCallback;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
initNetworkChecking();
}
private void initNetworkChecking() {
mNetworkStateCallback = new NetworkStateReceiver();
mNetworkStateCallback.setNetworkStateListener(new NetworkStateListener() {
@Override
public void onNetChange(int status) {
// TODO Auto-generated method stub
switch (status) {
case NetworkUtil.TYPE_WIFI:
Functions.toastString("have wifi connection", getActivity());
break;
case NetworkUtil.TYPE_MOBILE:
Functions.toastString("have 3g connection", getActivity());
break;
case NetworkUtil.TYPE_NOT_CONNECTED:
Functions.toastString("no connection", getActivity());
break;
default:
break;
}
}
});
}
}

在 list 中:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
<receiver android:name=".NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>

在查看 logcat 后,我​​相信当触发 NetworkStateReceiver.onReceiver 时,mOnNetworkStateListener 始终为 null。不知道哪里出错了?谁能帮我解决这个问题?提前谢谢大家。

最佳答案

您需要注册广播接收器。完全 FrgChoosing:

    private void initNetworkChecking() {
// TODO Auto-generated method stub
if (!Functions.hasConnection(getActivity())) {
Functions.toastString(getActivity().getResources().getString(R.string.alert_no_internet), getActivity());
}
mNetworkStateCallback = new NetworkStateReceiver();
mNetworkStateCallback.setNetworkStateListener(new NetworkStateListener() {
@Override
public void onNetChange(int status) {
// TODO Auto-generated method stub
switch (status) {
case NetworkUtil.TYPE_WIFI:
Functions.toastString("co mang wifi", getActivity());
break;
case NetworkUtil.TYPE_MOBILE:
Functions.toastString("co mang 3g", getActivity());
break;
case NetworkUtil.TYPE_NOT_CONNECTED:
Functions.toastString("mat mang roi", getActivity());
break;
default:
break;
}
}
});
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
getActivity().registerReceiver(mNetworkStateCallback, filter);
}

关于android - 无法在我的监听器和广播接收器中涉及接口(interface)方法空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972578/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com