对话框不显示
DialogBox not displaying
当点击保存 button
时,我希望它显示一个对话框,其中有 ok
和 cancel
button
用于确认。如果单击 ok
button
,将插入数据,然后是 "Data Saved" 消息。如果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();
}
});
}
尝试替换这一行:
AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
这个:
AlertDialog builder=new AlertDialog.Builder(WorkDetailsTable.this).create();
当点击保存 button
时,我希望它显示一个对话框,其中有 ok
和 cancel
button
用于确认。如果单击 ok
button
,将插入数据,然后是 "Data Saved" 消息。如果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();
}
});
}
尝试替换这一行:
AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext());
这个:
AlertDialog builder=new AlertDialog.Builder(WorkDetailsTable.this).create();