2017-09-03 68 views
-2

我想點擊正面按鈕顯示烤麪包時的警告對話框,但相反,它顯示一個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

使用YourActivity.thisgetApplicationContext(),而不是隻在thismakeText

0

必須正確使用語境中的第一個參數。取而代之的是,在創建Toast時使用dialog.getContext()或NameOfYourActivity.this。

相關問題