我想要点击肯定按钮以显示吐司时的警告对话框,而是显示一个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)
答案 0 :(得分:2)
在YourActivity.this
getApplicationContext()
或this
而非makeText
答案 1 :(得分:0)
您必须正确使用Context。而不是这个,在创建Toast时使用dialog.getContext()或NameOfYourActivity.this。