从警报对话框正面按钮制作吐司

时间:2017-09-03 13:04:23

标签: android alertdialog android-toast

我想要点击肯定按钮以显示吐司时的警告对话框,而是显示一个erorr!

代码

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Create new ?")
            .setMessage(" Do you want to create new msg ?!")
            .setPositiveButton("Yes ? ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(this," Ready to Create New Msg",Toast.LENGTH_LONG).show();

                }
            });

错误消息

Error:(38, 30) error: no suitable method found for makeText(<anonymous 
OnClickListener>,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to 
Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to 
Context)

2 个答案:

答案 0 :(得分:2)

YourActivity.this

的第一个参数中使用getApplicationContext()this而非makeText

答案 1 :(得分:0)

您必须正确使用Context。而不是这个,在创建Toast时使用dialog.getContext()或NameOfYourActivity.this。