【问题标题】:builder cannot be applied android (Alertdialog) [duplicate]builder无法应用android(Alertdialog)[重复]
【发布时间】:2017-10-31 16:54:00
【问题描述】:

我创建了一个 AlertDialog,它在 Android Studio 中显示 3 个按钮,但在保留字“this”上出现错误。在附图中,可以更详细地查看源代码。

我该如何解决这个错误?

private void muestraDialogo2() {

    AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
    builder2.setTitle("Lista de muestra");
    builder2.setMessage("Tipos de muestra");
    builder2.setCancelable(false);

    builder2.setPositiveButton("Imprimir todas", new DialogInterface.OnClickListener() {


        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(Main3Activity.this, "Imprimiendo...*", Toast.LENGTH_SHORT).show();

        }


    });

【问题讨论】:

  • 您可能想了解更多细节。提供您在做什么、为什么这样做等。
  • 为什么把这个方法放在 OnClickListener 匿名类体内?你懂java吗?

标签: android this android-alertdialog


【解决方案1】:

您在 AlertDialog.Builder 构造函数中传递了错误的参数。在第 50 行,this 对象引用 View.OnClickListener 类而不是有效的上下文对象。

换行:

AlertDialog.Builder builder2 = new AlertDialog.Builder(this);

AlertDialog.Builder builder2 = new AlertDialog.Builder(Main3Activity.this);

【讨论】:

  • 非常感谢。我纠正了错误。
  • @Airbeat 太棒了!请接受我的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2015-05-26
  • 2016-10-25
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多