代码一直在运行,直到我实现alertdialog活动。该活动在打开代码时会中断。
类和oncreate函数:
public class NewTask extends Activity implements OnCheckedChangeListener {
AlertDialog error = new AlertDialog.Builder(this).create();
Button Done, Date;
Task newtask=new Task();
TextView dateview,prob;
EditText name,desc;
RadioGroup prio;
int year,month,day;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_task);
initialize();
Setdate();
done();
}
对话框的用法
prio.setOnCheckedChangeListener(this);
Done.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
newtask.name=name.getText().toString();
newtask.discrb=desc.getText().toString();
if(newtask.name.length()==0||newtask.day==0||newtask.periocheck==0||newtask.discrb.length()==0)
{
error.setTitle("ERROR");
error.setMessage("Please fill all the fields!!!");
error.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
error.show();
}
else if(newtask.year<c.get(Calendar.YEAR)||(newtask.year==c.get(Calendar.YEAR)&&(newtask.month)<c.get(Calendar.MONTH))||(newtask.year==c.get(Calendar.YEAR)&&(newtask.month)==c.get(Calendar.MONTH)&&newtask.day<c.get(Calendar.DAY_OF_MONTH)))
{
error.setTitle("ERROR");
error.setMessage("Choose an existing day!!!");
error.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
error.show();
}
所以请帮我找出错误
答案 0 :(得分:0)
添加:
if(newtask.name.length()==0||newtask.day==0||newtask.periocheck==0||newtask.discrb.length()==0)
{
DialogFragment error = new ErrorMessage();
error.show(getSupportFragmentManager(), "Error");
}
像这样创建ErrorMessage
类:
public class ErrorMessage extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Choose an existing day!!!");
...//Add title buttons here
return builder.create();
}
}
祝你好运!