简体   繁体   English

如何关闭带有异步任务的进度对话框,并避免“您的活动是否运行错误?”

[英]How do I dismiss a progress dialog with async task and avoid “is your activity running error?”

I'm having trouble avoiding a "Unable to add window — token android.os.BinderProxy is not valid; is your activity running?" 我无法避免出现“无法添加窗口-令牌android.os.BinderProxy无效;您的活动正在运行吗?” exception when using a progress dialog box with an async task. 将进度对话框与异步任务一起使用时发生异常。

final ProgressDialog nDialog = new ProgressDialog(MainActivity.this);
    nDialog.setMessage("Loading...");
    nDialog.setIndeterminate(false);
    nDialog.setCancelable(false);
    if(!isFinishing()){nDialog.show();}

I then continue with: 然后,我继续:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
           //Run code while showing progress dialog
           ndialog.dismiss
}
    }, 1000);

I've come to understand that I'll occasionally get an exception because the activity will have finished, while the ndialog is being accessed. 我了解到有时会遇到异常,因为在访问ndialog的同时该活动将完成。 So a solution seems to be change the ndialog. 因此,解决方案似乎是更改ndialog。

The initial issue of showing a dialog once the activity finishes (which only happens rarely) is solved by 活动结束(很少发生)后显示对话框的最初问题是通过解决

if(!isFinishing()){nDialog.show();}

I was considering putting this same code for the nDialog.dismiss. 我正在考虑将相同的代码用于nDialog.dismiss。 But the problem is that if I do: 但是问题是,如果我这样做:

if(!isFinishing()){nDialog.dismiss();}

and the activity finishes before this can run, the user will get stuck with a dialog screen that will never get dismissed. 并且活动在此运行之前完成,用户将陷入一个对话框,而该对话框永远不会被解雇。

Am I missing something? 我想念什么吗? How can I prevent this error, but at the same time make sure that the dialog will start and be dismissed? 如何防止出现此错误,但同时确保对话框将启动并被关闭?

Thanks! 谢谢!

Careful while handling views, activities and other related UI objects from another thread. 在从另一个线程处理视图,活动和其他相关的UI对象时要小心。 Threads like AsyncTask are not aware of the activity lifecycle, and you may end up posting things to dead windows. 诸如AsyncTask之类的线程不知道活动生命周期,因此您可能最终将内容发布到无效的窗口中。 I believe this is what is happening to you. 我相信这就是您正在发生的事情。

A safer way to do this: 一种更安全的方法:

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.lang.ref.WeakReference;

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

private final WeakReference<MyActivity> weakReferenceActivity;

public MyTask(@NonNull MyActivity activity) {
    this.weakReferenceActivity = new WeakReference<>(activity);
}

@Nullable
public MyActivity getActivity() {
    MyActivity activity = weakReferenceActivity.get();
    if (activity.isDestroyed()) {
        return null;
    }
    return activity;
}

@Override
protected void onPreExecute() {
    MyActivity activity = getActivity();
    if (activity != null) {
        activity.showProgressDialog();
    }
}

@Override
protected Void doInBackground(Void... voids) {
    [do something]
    return null;
}

@Override
protected void onPostExecute(Void nothing) {
    MyActivity activity = getActivity();
    if (activity != null) {
        activity.hide();
    }
}
}

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

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