异步任务中进度对话框的偶发错误

时间:2017-03-24 23:10:17

标签: android

我有处理文件异步的代码。据我所知,我正在打开一个流程对话框。但android表示执行前函数存在错误

我的代码

private class asyncparsefile extends AsyncTask<String, Void, Boolean> {

    int avg;
    int max;
    int min;
    int totalwords;
    int totalsentences;
    HashMap<String, Integer> words;
    HashMap<String, Integer> poscount;
    private ProgressDialog dialogp;

    /** progress dialog to show user that the backup is processing. */
    /**
     * application context.
     */
    @Override
    protected void onPreExecute() {
        if (dialogp != null){
            dialogp.dismiss();
        }
        dialogp =  new ProgressDialog(MainActivity.this);
        dialogp.setMessage("Please wait");
        dialogp.show();
    }

    @Override
    protected Boolean doInBackground(final String... args) {
        Helper help = new Helper(filepath, getApplicationContext());
        avg = help.avgsentence();
        max = help.maxsentence();
        min = help.minsentence();
        words = help.individualWordCounts();
        totalsentences = help.sentencecount();
        totalwords = help.wordcount();
        if (isCheck) {
            Languageanalytics ana = new Languageanalytics(help.getSentences(), getApplicationContext());
            poscount = ana.countPOS();

        }

        return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        super.onPostExecute(success);
        dialogp.dismiss();
        dialogp = null;

        //TODO Add results page, then this thing should throw to said page
        Intent intent = new Intent(getBaseContext(), ResultsActivity.class);
        intent.putExtra("Average", avg);
        intent.putExtra("Max", max);
        intent.putExtra("Min", min);
        intent.putExtra("Map_counting_of_individual_words", words);
        intent.putExtra("Sentences_in_Document", totalsentences);
        intent.putExtra("Words_in_Document", totalwords);
        if (isCheck){
            intent.putExtra("POSCount", poscount);
        }

        startActivity(intent);
    }
}

它产生的错误。这个错误似乎一直弹出的原因是什么?任何帮助将不胜感激

WindowManager: android.view.WindowLeaked: Activity textsnoop.rddigi.com.textstats.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41f322c0 V.E..... R......D 0,0-665,96} that was originally added here
                                                                             at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
                                                                             at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
                                                                             at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                                                                             at android.app.Dialog.show(Dialog.java:286)
                                                                             at textsnoop.rddigi.com.textstats.MainActivity$asyncparsefile.onPreExecute(MainActivity.java:135)
                                                                             at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
                                                                             at android.os.AsyncTask.execute(AsyncTask.java:535)
                                                                             at textsnoop.rddigi.com.textstats.MainActivity$2.onClick(MainActivity.java:75)
                                                                             at android.view.View.performClick(View.java:4445)
                                                                             at android.view.View$PerformClick.run(View.java:18446)
                                                                             at android.os.Handler.handleCallback(Handler.java:733)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5146)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
                                                                             at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

通常,当您尝试在活动销毁时显示对话框时会发生这种情况。 看看这个答案:https://stackoverflow.com/a/32065936/802034

答案 1 :(得分:0)

事实证明问题是我没有捕获空指针异常。在try catch块中添加,找到并解决了问题。