Bad window token, you cannot show a dialog before an Activity is created or after it's hidden

前端 未结 5 2026
有刺的猬
有刺的猬 2021-02-03 22:02

I am using an AppIntro library in my app.

It has 3 slides. I want to ask the user something when the third slide is shown. To achieve that I am using material dialogs b

相关标签:
5条回答
  • 2021-02-03 22:41
    MaterialDialog dialog = new MaterialDialog.Builder(getApplicationContext())
    

    I suppose you need to pass the Activity instance here, not the application context.

    0 讨论(0)
  • 2021-02-03 22:43

    Use "this" instead of "getApplicationContext()" if this error happening even inside the activity.

        mProgress = new MaterialDialog.Builder(this)
    
    0 讨论(0)
  • 2021-02-03 22:47

    This can be resolved by confirming that current Activity hasWindowFocus because when you get the context in the fragment, instead of exactly which Activity it is, it might not have windowFocus :

    if (((Activity) mContext).hasWindowFocus()) {
            mDialog.show();
    }
    
    0 讨论(0)
  • 2021-02-03 22:47
    public class MainActivity extend Activity{
    
        MaterialDialog dialog;
    
        protected void onCreate(Bundle savedInstanceState) {
    
            dialog =  new MaterialDialog.Builder(OrderInfoActivity.this)
                    .title("Reject?")
                    .content("Are you reject this order?")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            Toast.makeText(OrderInfoActivity.this, "Option1", Toast.LENGTH_SHORT).show();
                            }
                    })
                    .onNeutral(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            Toast.makeText(OrderInfoActivity.this, "Option 2", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            Toast.makeText(OrderInfoActivity.this, "Option 3", Toast.LENGTH_SHORT).show();
                        }
                    }).build();
    
            accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                      dialog.show();
                }
            });
        }
    }
    

    Here is what i'm using right now, and it's working

    Hope it helps :)

    0 讨论(0)
  • 2021-02-03 22:48

    use

     MaterialDialog dialog = new MaterialDialog.Builder(YourActivityName.class)
    

    do not use getApplicationContext().

    0 讨论(0)
提交回复
热议问题