我正在使用自定义动画来显示进度对话框。它会因此错误而崩溃。如何在自定义类中修复此问题?
错误:
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();
}
}
答案 0 :(得分:1)
当调用对话框的活动因某种原因在尝试显示对话框时完成时。这是为我解决的问题:
if(!((Activity) context).isFinishing())
{
dialog.show();
}
只需检查您的活动是否完成,然后实施对话框。