简体   繁体   English

断开Internet连接时,请妥善处理NPE并在OnPostExecute / AsyncTask中显示AlertDialog-Android / Java

[英]Handle NPE Gracefully and Display AlertDialog in OnPostExecute / AsyncTask When Internet Connection Is Lost - Android / Java

I'm getting a NPE (as expected) in my AsyncTask during a loss of internet connectivity. 断开互联网连接时,我的AsyncTask中出现了NPE(如预期的那样)。 I've attempted to do a bit of null checking then display an alertDialog if the result is null - however I the program is still crashing and is not stopping gracefully. 我试图做一些空检查,如果结果为空,则显示一个alertDialog-但是,我的程序仍然崩溃,不能正常停止。 How might this be avoided / what am I missing in this code? 如何避免这种情况/我在代码中缺少什么?

Source: 资源:

@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();

    }
}

LogCat: logcat的:

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): -------------------

Replace 更换

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

with

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

EDIT : you need to handle it in activity itself as mentioned: 编辑:您需要如上所述在活动本身中处理它:

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

and

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;
                            }

I do not know the exact line of the NPE raised, but the problem is probably related to this line: 我不知道所提出的NPE的确切数字,但问题可能与这条数字有关:

new AlertDialog.Builder(null);

You cant pass null here. 您不能在此处传递null。 You should put an instance of android.content.Context here (your Activity for example which started this task). 您应该在此处放置android.content.Context的实例(例如,您的Activity发起了此任务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM