gpt4 book ai didi

java - 当互联网连接丢失时,优雅地处理 NPE 并在 OnPostExecute/AsyncTask 中显示 AlertDialog - Android/Java

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

在互联网连接丢失期间,我的 AsyncTask 中出现了 NPE(如预期)。我尝试进行一些空检查,然后在结果为空时显示一个警报对话框 - 但是程序仍然崩溃并且没有正常停止。如何避免这种情况/我在这段代码中缺少什么?

来源:

@Override
protected void onPostExecute(FunctionResult result) {
if (result != null) {
listener.callback(result);
}
else {
String errorMsg = "Failed";
AlertDialog.Builder builder = new AlertDialog.Builder(null);
builder.setMessage(errorMsg)
.setTitle("Error")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();

}
}

日志猫:

05-27 17:13:11.250: E/AndroidRuntime(23343): FATAL EXCEPTION: main
05-27 17:13:11.250: E/AndroidRuntime(23343): java.lang.NullPointerException
05-27 17:13:11.250: E/AndroidRuntime(23343): at com.example.login.wstasks.WebTask.onPostExecute(WebTask.java:65)
05-27 17:13:11.250: E/AndroidRuntime(23343): at com.example.login.wstasks.WebTask.onPostExecute(WebTask.java:1)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.os.Looper.loop(Looper.java:137)
05-27 17:13:11.250: E/AndroidRuntime(23343): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 17:13:11.250: E/AndroidRuntime(23343): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 17:13:11.250: E/AndroidRuntime(23343): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 17:13:11.250: E/AndroidRuntime(23343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 17:13:11.250: E/AndroidRuntime(23343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 17:13:11.250: E/AndroidRuntime(23343): at dalvik.system.NativeStart.main(Native Method)
05-27 17:13:21.240: V/TaskManager(23343): In executeTask() on BACKGROUND THREAD
05-27 17:13:21.290: D/dalvikvm(23343): GC_CONCURRENT freed 406K, 9% free 4779K/5224K, paused 4ms+3ms, total 31ms
05-27 17:13:21.340: V/TaskManager(23343): In logTaskInfo() on BACKGROUND THREAD
05-27 17:13:21.340: V/TaskManager(23343): -------------------
05-27 17:13:21.340: V/TaskManager(23343): -------------------

最佳答案

替换

AlertDialog.Builder builder = new AlertDialog.Builder(null);

AlertDialog.Builder builder = new AlertDialog.Builder(YOUR_ACTIVITY_NAME.this);

编辑:您需要在 Activity 本身中处理它,如上所述:

protected void onPostExecute(FunctionResult result) {
listener.callback(result);
}

KeepAliveData data = new KeepAliveData();
FunctionResult keepAliveResponse = null;
try {
keepAliveResponse = new KeepAliveTask(variables.login, data)
.execute().get();




if (keepAliveResponse == null) {
String errorMsg = "KeepAlive Failed";
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityMain.this);
builder.setMessage(errorMsg)
.setTitle("Error")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();

}

} catch (Exception e) {
e.printStackTrace();
return false;
}

关于java - 当互联网连接丢失时,优雅地处理 NPE 并在 OnPostExecute/AsyncTask 中显示 AlertDialog - Android/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899281/

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