gpt4 book ai didi

java - 网络回调onAvailable方法中接口(interface)空指针异常

转载 作者:行者123 更新时间:2023-12-02 00:15:44 24 4
gpt4 key购买 nike

下面是我的代码,我尝试实现接口(interface)来拦截来自 Connectivity Manager 网络回调的响应,但 Android Studio 在 onAvailable 方法中的该接口(interface) internetListener.onInternetStatusUpdate(true) 上抛出错误。

我还在我的主要 Activity 中实现了这个界面。我也遵循了一些例子,但失败了。是不是那个onAvailable方法,我的接口(interface)无法访问?

public class CheckInternetAsyncTask extends ConnectivityManager.NetworkCallback
{
private static final String TAG = "CheckInternetAsyncTask";
private Context context;
public InternetListener internetListener;
private ConnectivityManager connectivityManager;
private Network network;
private NetworkCapabilities networkCapabilities;

public interface InternetListener{
void onInternetStatusUpdate(boolean hasInternet);
}

public CheckInternetAsyncTask(Context _context) {
this.context = _context;
this.connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(this);
}

try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}

public void TestInternetRequest(){
try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}


@Override
public void onAvailable(Network network) {
super.onAvailable(network);
this.network = network;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.bindProcessToNetwork(network);
this.network = connectivityManager.getActiveNetwork();
}else{
ConnectivityManager.setProcessDefaultNetwork(network);
}

this.networkCapabilities = connectivityManager.getNetworkCapabilities(network);


if(networkCapabilities != null && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)){
internetListener.onInternetStatusUpdate(true);
}else{
try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}

class SendInternetRequest extends AsyncTask<Void,Void, Void>{
private static final String TAG = "SendInternetRequest";

CheckInternetAsyncTask.InternetListener internetListener;

@Override
protected Void doInBackground(Void... voids) {
try {
Socket sock = new Socket();
sock.connect(new InetSocketAddress("8.8.8.8", 53), 1500);
sock.close();
internetListener.onInternetStatusUpdate(true);

} catch (IOException e) {
internetListener.onInternetStatusUpdate(false);
}

return null;
}
}

堆栈跟踪

2019-09-25 10:08:02.566 29555-29578/com.kioskactionandnotification E/AndroidRuntime: FATAL EXCEPTION: ConnectivityThread
Process: com.kioskactionandnotification, PID: 29555
java.lang.NullPointerException: Attempt to invoke interface method 'void com.kioskactionandnotification.Model.Helper.CheckInternetAsyncTask$InternetListener.onInternetStatusUpdate(boolean)' on a null object reference
at com.kioskactionandnotification.Model.Helper.CheckInternetAsyncTask.onAvailable(CheckInternetAsyncTask.java:85)
at android.net.ConnectivityManager$NetworkCallback.onAvailable(ConnectivityManager.java:2770)
at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:2969)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)

MainActivity.java

我实现了接口(interface)

@Override
public void onInternetStatusUpdate(boolean hasInternet) {
this.hasInternet = hasInternet;
Log.d(TAG, "onInternetStatusUpdate: hasInternet : "+hasInternet);
}

这就是我所拥有的一切。

最佳答案

您似乎正在尝试访问 internetListener 类型的未初始化对象。

您需要初始化它:

internetListener = new InternetListener();

或者检查它是否已经初始化:

if (internetListener != null) {
internetListener.onInternetStatusUpdate(true);
}

注意

您使用的任何抽象类都应该由子类扩展,该子类为抽象类中使用的任何抽象数据/方法提供实现。

Abstract class in Java

关于java - 网络回调onAvailable方法中接口(interface)空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090070/

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