创建警报对话框构建时出错

时间:2015-11-20 14:28:38

标签: android

我正在尝试在我的活动中创建一个警告对话框构建器。但出于某些原因,当我在AlertDialog.Builder(this)中使用它时,它无法识别"这个"宾语。 即使我将其更改为getAppllicationContext()或getApplication(),应用程序也会崩溃。 这是我的代码:

new AlertDialog.Builder(this)
                    .setTitle("Date Error!")
                    .setMessage("Date is not valid! Please select another date")
                    .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setIcon(R.drawable.error_32)
                    .show();

1 个答案:

答案 0 :(得分:1)

我认为你的代码看起来有点像这样:

class AlertClass extends Activity {
    ...setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            new AlertDialog.Builder(...
        }
    });
}

只做

new AlertDialog.Builder(AlertClass.this);