简体   繁体   English

对话框不显示

[英]DialogBox not displaying

When the save button clicked, I want it display a dialog box, which has ok and cancel button for confirmation. 单击保存button ,我希望它显示一个对话框,其中包含okcancel button以进行确认。 If ok button is clicked, data will be inserted follow by "Data Saved" message. 如果单击ok button ,将插入数据,然后显示“已保存数据”消息。 If cancel , it will dismiss. 如果cancel ,它将关闭。

I try working out with sample code, but when I click on save button , the app crashed without showing any logcat error..how can I fix this? 我尝试使用示例代码,但是当我单击“ save button ,应用程序崩溃了,但没有显示任何logcat错误。如何解决此问题?

WorkDetailsTable.java WorkDetailsTable.java

 Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
                builder.setTitle("Data Saved");
                builder.setMessage("Are you sure you want to save?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {
                        W1 = txtWork1.getText().toString();
                        W2 = txtWork2.getText().toString();
                        W3 = txtWork3.getText().toString();
                        W4 = txtWork4.getText().toString();
                        a1 = spinnerTra.getSelectedItem().toString();
                        a2 = spinnerTra2.getSelectedItem().toString();
                        a3 = spinnerTra3.getSelectedItem().toString();
                        a4 = spinnerTra4.getSelectedItem().toString();
                        P1 = per1.getText().toString();
                        P2 = per2.getText().toString();
                        P3 = per3.getText().toString();
                        P4 = per4.getText().toString();
                        long ab = ts.insertTimeSheet(name, weather,date,status);
                        WF.insertWorkForce(subContractors, noPeople, noHours);
                        WD.insertWorkDetails(a1, W1, P1, b, c, th, ab);
                        WD.insertWorkDetails(a2, W2, P2, d, e1, th, ab);
                        WD.insertWorkDetails(a3, W3, P3, f, g, th, ab);
                        WD.insertWorkDetails(a4,W4,P4,h,i,th,ab);
                        Toast.makeText(context, "Data Saved", Toast.LENGTH_SHORT).show();

                   }

                });
                builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog,int ii)
                    {
                        dialog.dismiss();
                }
            });
               builder.show();


            }
        });
    }

Try replace this line: 尝试替换此行:

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

with this one: 与此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM