自定义进度对话框崩溃

custom progress dialog is crashing

我正在使用自定义动画来显示进度 dialog.It 有时因此错误而崩溃。如何在自定义 class?

中解决此问题

错误:

Unable to add window -- token android.os.BinderProxy@37ac524 is not valid; is your activity running?

代码:

public class MyCustomProgressDialog extends ProgressDialog {
    private AnimationDrawable animation;

    public static ProgressDialog ctor(Context context) {
        MyCustomProgressDialog dialog = new MyCustomProgressDialog(context);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        return dialog;
    }

    public MyCustomProgressDialog(Context context) {
        super(context);
    }

    public MyCustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_custom_progress_dialog);

        ImageView la = (ImageView) findViewById(R.id.animation);
        la.setBackgroundResource(R.drawable.custom_progress_dialog_animation);
        animation = (AnimationDrawable) la.getBackground();
    }

    @Override
    public void show() {
        super.show();
        animation.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        animation.stop();
    }
}

编辑 2:

public class MyCustomProgressDialog extends ProgressDialog {
    private AnimationDrawable animation;

    Context ctx;

    public ProgressDialog ctor(Context context) {
        ctx= context;

        MyCustomProgressDialog dialog = new MyCustomProgressDialog(context);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        return dialog;
    }

    public MyCustomProgressDialog(Context context) {
        super(context);
        ctx= context;
    }

    public MyCustomProgressDialog(Context context, int theme) {
        super(context, theme);
        ctx= context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_custom_progress_dialog);

        ImageView la = (ImageView) findViewById(R.id.animation);
        la.setBackgroundResource(R.drawable.custom_progress_dialog_animation);
        animation = (AnimationDrawable) la.getBackground();
    }

    @Override
    public void show() {

        if(!((Activity) ctx).isFinishing())
        {
            //show dialog

            super.show();
            animation.start();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        animation.stop();
    }
}

当调用对话框的 activity 尝试显示对话框时出于某种原因或其他原因完成时。这是为我解决的问题:

if(!((Activity) context).isFinishing())
{
    dialog.show();
}

只需检查您的 activity 是否完成,然后执行对话框。