[英]Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
[英]Android: package android.support.v7.app.AlertDialog does not exist
所以我試圖實現一個條形碼掃描儀功能我有這段代碼
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;
試試這個代碼。 我希望這會奏效。 如果沒有導入 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.