簡體   English   中英

警報對話框上的錯誤

[英]Error in Alert dialog on this

這是我的代碼

b3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if (v == b3) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Delete")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            mydb.deleteContact(id_To_Update);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            AlertDialog d = builder.create();
            d.setTitle("Are you sure");
            d.show();
        }
    }

});

if (!rs.isClosed()) {
    rs.close();
}
name.setText(nam);
email.setText(emai);

當我在刪除按鈕上使用此代碼時,第一個“ this”出現錯誤。 怎么解決呢? 如何使用刪除確認消息?

請幫我。 提前致謝!

使用上下文而不是簡單的“ this”引用。 在這里,您可以使用“活動”上下文,而不能使用任何其他上下文。 有關更多信息,請訪問以下鏈接ContextAlertDialog.Builder

活動:

 AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);

對於片段:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

謝謝

您需要更換

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

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM