gpt4 book ai didi

java - 当我的在线状态发生变化时,Android 广播接收器崩溃

转载 作者:行者123 更新时间:2023-12-01 13:29:59 25 4
gpt4 key购买 nike

我扩展了一个广播接收器,以便我的应用程序知道在线状态何时发生变化。问题是:每当在线状态发生变化时,我的广播接收器似乎都会被重建。因为我在构造函数之后添加了一个监听器,所以我的监听器为空,并且出现 NullPointerException:

02-07 11:11:45.804: E/AndroidRuntime(21416): FATAL EXCEPTION: main
02-07 11:11:45.804: E/AndroidRuntime(21416): java.lang.RuntimeException: Unable to start receiver nl.raakict.android.dnd.util.NetworkStateReceiver: java.lang.NullPointerException
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2699)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.app.ActivityThread.access$1500(ActivityThread.java:162)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1453)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.os.Looper.loop(Looper.java:158)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.app.ActivityThread.main(ActivityThread.java:5751)
02-07 11:11:45.804: E/AndroidRuntime(21416): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:11:45.804: E/AndroidRuntime(21416): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 11:11:45.804: E/AndroidRuntime(21416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
02-07 11:11:45.804: E/AndroidRuntime(21416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
02-07 11:11:45.804: E/AndroidRuntime(21416): at dalvik.system.NativeStart.main(Native Method)
02-07 11:11:45.804: E/AndroidRuntime(21416): Caused by: java.lang.NullPointerException
02-07 11:11:45.804: E/AndroidRuntime(21416): at nl.raakict.android.dnd.util.NetworkStateReceiver.onReceive(NetworkStateReceiver.java:38)
02-07 11:11:45.804: E/AndroidRuntime(21416): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2687)
02-07 11:11:45.804: E/AndroidRuntime(21416): ... 10 more

我的自定义广播接收器如下所示:

package nl.raakict.android.dnd.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkStateReceiver extends BroadcastReceiver{
private Listener mListener = null;
private static NetworkStateReceiver _instance;

public static NetworkStateReceiver getInstance()
{
if (_instance == null)
{
_instance = new NetworkStateReceiver();
}
return _instance;
}

public NetworkStateReceiver(){
}

@Override
public void onReceive(Context context, Intent intent) {
Log.d("app","Network connectivity change");
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
Log.i("app","Network "+ni.getTypeName()+" connected");
mListener.onStateChange(true);
}
}
if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
Log.d("app","There's no network connectivity");
mListener.onStateChange(false);
}
}

public interface Listener {
public void onStateChange(boolean state);
}

public void registerListener (Listener listener) {
mListener = listener;
}

public boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

}

和我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.raakict.android.dnd"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="nl.raakict.android.dnd.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="nl.raakict.android.dnd.util.NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>

这是我的监听器类:

public class RequestHandler implements NetworkStateReceiver.Listener {

private static RequestHandler _instance;
private NetworkStateReceiver networkStateReveiver;
private ArrayList<CustomJsonRequest> delayedRequestQueue;
private boolean currentlyOnline;
private Context applicationContext;


public static RequestHandler getInstance(Context applicationContext)
{
if (_instance == null)
{
_instance = new RequestHandler(applicationContext);
}
return _instance;
}

private RequestHandler(Context applicationContext){
delayedRequestQueue= new ArrayList<CustomJsonRequest>();
this.applicationContext = applicationContext;
networkStateReveiver = NetworkStateReceiver.getInstance();
networkStateReveiver.registerListener(this);
currentlyOnline = networkStateReveiver.isOnline(applicationContext);
}

@Override
public void onStateChange(boolean online) {
// TODO Auto-generated method stub
currentlyOnline = online;
if(online){
sendRequestQueue();
}
}

public void newPostRequest(CustomJsonRequest request) {
delayedRequestQueue.add(request);
if(currentlyOnline)
sendRequestQueue();
}

public void sendRequestQueue(){
//code logic here
}
}

我做错了什么?

最佳答案

问题是,当网络状态完成时,Android 将创建您的 NetworkStateReceiver 类实例,调用其 onReceive,然后销毁您的接收器实例。因此,这就是为什么您的监听器始终为空的原因,如果您想通知网络更改的某些其他组件,那么您应该在应用程序中发送本地广播。

您还可以注册本地广播接收器:

“以编程方式注册您的广播接收器”: http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/

关于java - 当我的在线状态发生变化时,Android 广播接收器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625138/

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