2017-09-03 13 views
-2

トーストを表示するためにポジティブボタンをクリックするとアラートダイアログが表示されますが、代わりにエラーが表示されます!アラートダイアログからトーストするポジティブボタン

コード

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(); 

       } 
      }); 

エラーMSG

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.thisかだけではなくthisあなたは正しくコンテキストを使用する必要がありますmakeText

0

の最初のパラメータでのgetApplicationContext()。これの代わりに、dialog.getContext()またはNameOfYourActivity.thisを使用してToastを作成します。

関連する問題