2012-01-11 17 views
13

私はonClickListenerからAlertDialogを開始しようとしていますが、次のエラーが発生しています。onClickListener内のAlertDialog

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined 

これを修正する方法を知っている人はいますか?コンストラクタは、あなたがあなたの活動のオブジェクトを使用するコンテキストタイプ& OnclickListner is not a Context typeを必要とするため

 mRecordButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      new AlertDialog.Builder(this) 
      .setTitle("Cast Recording") 
      .setMessage("Now recording your message") 
      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Log.d("AlertDialog", "Positive"); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Log.d("AlertDialog", "Negative"); 
       } 
      }) 
      .show(); 
     } 
    }); 

答えて

29

変更このライン

new AlertDialog.Builder(this); 

new AlertDialog.Builder(YourActivity.this); 

にこれがあります。私はそれが役に立てば幸い

.. new AlertDialog.Builder(this)

0

thisはリスナーではなく、外側のクラスのインスタンスを参照しています。