繁体   English   中英

不调用onPostExecute

[英]onPostExecute is not called

所以我的AsyncTask遇到了问题。 如果在doInBackground捕获了某些可抛出的错误,我需要postExecute来显示警报对话框。 问题在于,永远不会调用postExecute 我曾尝试添加@Override但Android Studio表示它没有覆盖其超类中的方法。 我也尝试过更改返回类型。 我环顾了这个网站,找不到答案。 提前致谢。

AsyncTask类

public class AsyncTaskActivity extends AsyncTask<Void, Void, Void> {

    String exception;

    @Override
    protected void onPreExecute() {
    }


    protected void onPostExecute() {
        if (exception.contains("java.net.UnknownHostException")) {
            MainActivity.showDialog();
            Log.i("Error Message", "ERROR MESSAGE SHOWN");
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Log.i("AsyncTask", "Loading...");
            // Make a URL to the web page. This takes the string representation of the URL
            // and changes it into a URL object
            URL url = new URL("http://api.wunderground.com/api/0c0fcc3bf62ab910/conditions/q/IN/Fort_Wayne.json");

            // Get the input stream through URL Connection
            URLConnection con = url.openConnection();
            InputStream is = con.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            String line;
            // read each line and write to text file
            while ((line = br.readLine()) != null) {
                Log.i("AsyncTask", line);
                TextEditor.file = new File(MainActivity.path, "siteInfo.txt");
                TextEditor.writeString(line);
            }
            TextEditor.saveAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            exception = e.toString();
        }

        Log.i("AsyncTask", "DONE");
        return null;
    }
}

showDialog方法

public static void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.context);
    builder.setView(R.layout.dialog_layout);
    builder.setPositiveButton(
            R.string.dialog_close,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    System.exit(1);
                }
            });

    builder.show();
}

看起来你在想什么

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    if (exception.contains("java.net.UnknownHostException")) {
        MainActivity.showDialog();
        Log.i("Error Message", "ERROR MESSAGE SHOWN");
    }
}

首先,请参阅此文档。 您错过了onPostExecute()上的参数。

https://developer.android.com/reference/android/os/AsyncTask.html

您要做的是,

@Override
protected void onPostExecute(Params) {
        // your logics 
}
@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    if (exception.contains("java.net.UnknownHostException")) {
        MainActivity.showDialog();
        Log.i("Error Message", "ERROR MESSAGE SHOWN");
    }
}

请注意,您需要初始化异常,否则可能会导致NullPointerException

暂无
暂无

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

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