DialogBox不显示

时间:2015-10-08 17:49:12

标签: java android button toast dialog

点击保存button后,我希望它显示一个对话框,其中包含okcancel button以进行确认。如果点击了ok button,则会插入数据,然后按"数据保存"信息。如果cancel,则会解散。

我尝试使用示例代码,但是当我点击save button时,应用程序崩溃时没有显示任何logcat错误。我可以解决这个问题吗?

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();


            }
        });
    }

1 个答案:

答案 0 :(得分:0)

尝试替换此行:

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

这一个:

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