美文网首页
Unable to add window -- token nu

Unable to add window -- token nu

作者: 皮皮铭 | 来源:发表于2019-08-05 18:39 被阅读0次
     btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommomDialog commomDialog = new CommomDialog(getApplicationContext(), R.style.dialog, "是否要删除门店", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            Log.e(TAG, "onClick: "+confirm );
                            if (confirm){
                                Toast.makeText(getApplicationContext(),"删除",Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(getApplicationContext(),"取消删除",Toast.LENGTH_SHORT).show();
                            }
    
                            dialog.dismiss();
                        }
                    });
                    commomDialog.setTitle("提示").show();
                }
            });
    

    原因是因为Dialog 不能用getApplicationContext(),原因是因为Token必须是Activity的Token,不然的话会抛出异常

         btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommomDialog commomDialog = new CommomDialog(Test2Activity.this, R.style.dialog, "是否要删除门店", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            Log.e(TAG, "onClick: "+confirm );
                            if (confirm){
                                Toast.makeText(getApplicationContext(),"删除",Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(getApplicationContext(),"取消删除",Toast.LENGTH_SHORT).show();
                            }
    
                            dialog.dismiss();
                        }
                    });
                    commomDialog.setTitle("提示").show();
                }
            });
    

    相关文章

      网友评论

          本文标题:Unable to add window -- token nu

          本文链接:https://www.haomeiwen.com/subject/jhhvdctx.html