onClickListener中的AlertDialog

Car*_*ris 13 android android-alertdialog

我正在尝试从onClickListener启动AlertDialog但是我收到以下错误.

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这一问题?

        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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

R.d*_*vaw 30

改变这一行

new AlertDialog.Builder( this );
Run Code Online (Sandbox Code Playgroud)

new AlertDialog.Builder( YourActivity.this );
Run Code Online (Sandbox Code Playgroud)

这是因为构造函数需要Context类型,OnclickListner is not a Context type因此您使用Activity的对象.

我希望它有帮助..