2016-03-28 19 views
0

カスタムアニメーションを使用して進行状況ダイアログを表示しています。このエラーでいつかクラッシュしています。これをカスタムクラスでどのように修正できますか?カスタムプログレスダイアログがクラッシュする

エラー:

Unable to add window -- token [email protected] 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(); 
    } 
} 
+1

[ウィンドウを追加できません - トークンandroid.os.BinderProxyは無効です。あなたの活動は実行されていますか?](http://stackoverflow.com/questions/9529504/unable-to-add-window-token-android-os-binderproxy-is-not-valid-is-your-activ) –

答えて

1

それが表示するようにしようとしたとき、ダイアログを呼び出すアクティビティが何らかの理由または別のために仕上げました。ダイアログ。ここで私のために解決したものがあります:

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

アクティビティが終了しているかどうかを確認してから、ダイアログを実装してください。

+0

このコードをカスタムクラスに追加しますか? – jason

+0

@jasonこれを訪問するhttp://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/ –

+0

編集2で新しいコードを追加しました。これは使えますか? – jason

関連する問題