0

I am using custom animation to show progress dialog.It is crashing sometime with this error. How can I fix this in the custom class?

Error:

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

Code:

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

Edit 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();
    }
}
jason
  • 3,932
  • 11
  • 52
  • 123
  • 1
    Possible duplicate of [Unable to add window -- token android.os.BinderProxy is not valid; is your activity running?](http://stackoverflow.com/questions/9529504/unable-to-add-window-token-android-os-binderproxy-is-not-valid-is-your-activ) –  Mar 28 '16 at 07:39

1 Answers1

1

when the activity calling the dialog was finishing for some reason or another when it tried to show a dialog. Here's what solved it for me:

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

Just check your activity is finish or not and then implement dialog.

  • @jason visit this http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/ –  Mar 28 '16 at 07:44