首页 > 解决方案 > Android:包 android.support.v7.app.AlertDialog 不存在

问题描述

所以我试图实现一个条形码扫描仪功能我有这段代码

 private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new android.support.v7.app.AlertDialog.Builder(addquantityactivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

我得到这个错误error: package android.support.v7.app.AlertDialog does not exist new android.support.v7.app.AlertDialog.Builder(addquantityactivity.this)

值得一提的是,我使用 androidx 并且尝试了以下导入。stackoverflow 上的其他解决方案也没有帮助我。

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

标签: javaandroidandroid-alertdialogandroid-appcompat

解决方案


试试这个代码。我希望这会奏效。如果没有导入 AlertDialog 则重新导入androidx版本。

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(addquantityactivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

推荐阅读