2012-01-11 155 views
13

我试图从onClickListener启动AlertDialog,但出现以下错误。AlertDialog onClickListener

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

有没有人知道如何解决这个问题?

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

这是因为构造函数需要一个上下文类型& OnclickListner is not a Context type让你用你的活动的对象。

我希望它有助于..

0

new AlertDialog.Builder(this)this指的是听众,而不是外部类的实例。