首页 > 代码库 > 兔子--The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined

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

出错原因:Builder builder=new Builder(this) 该方法里面的this为当前类OnClickListener的实例,

而不是我们想要赋值的MainActivity.this,只需要把this改为MainActivity.this。



我的代码如下:

<span style="font-size:18px;">public class MainActivity extends Activity {
	private Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(MainActivity.this)
						.setTitle("添加好友")
						.setMessage("请输入要添加好友的帐户:")
						.setIcon(android.R.drawable.ic_dialog_info)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface arg0,
											int arg1) {
										// EChatAccountManager.getInstance()
										// .addContact(
										// new String[] { etAccount.getText()
										// .toString() }, "我要加你好有");
									}
								}).setNegativeButton("取消", null).show();

			}
		});
	}
</span>

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