在自定义对话框上使用DatePicker

时间:2011-11-13 15:19:44

标签: android datepicker

嗯,这就是我的代码。我想从自定义对话框中获取DatePicker中的值。当我尝试初始化dp(DatePicker)时,我得到了一个nullPointerException错误,我不能将DatePicker中的值带到变量。 :/任何人都知道发生了什么事? (我有1个布局。主要(R.layout.notifications)和对话框布局(R.layout.notifications_dialog),datepicker在notifications_dialog布局上。

public class Notifications extends Activity {

static final int CUSTOM_DIALOG_ID = 0;
EditText Notification, Freq;
Button Save, Cancel;
int Year, Month , Day ;
DatePicker dp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notifications); 
    Button ok = (Button) findViewById(R.id.ok);
    dp=(DatePicker)findViewById(R.id.notdate);


    ok.setOnClickListener(new OnClickListener() {                
             public void onClick(View ovuinfo) {
                showDialog(CUSTOM_DIALOG_ID);                
               }
           });
}

@Override
protected Dialog onCreateDialog(int id) {
 Dialog dialog = null;;
    switch(id) {
    case CUSTOM_DIALOG_ID:
     dialog = new Dialog(Notifications.this);

     dialog.setContentView(R.layout.notifications_dialog);
     dialog.setTitle("Add Notification");

     Notification = (EditText)dialog.findViewById(R.id.notification);
     Freq = (EditText)dialog.findViewById(R.id.freq);
     Save = (Button)dialog.findViewById(R.id.add);
     Cancel = (Button)dialog.findViewById(R.id.canc);
     dp=(DatePicker)findViewById(R.id.notdate);

     final Calendar cal = Calendar.getInstance();
     int year = cal.get(Calendar.YEAR);
     int monthOfYear = cal.get(Calendar.MONTH);
     int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

      // here is the problem!
      dp.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
         @Override
         public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
             Year = year;
             Month = monthOfYear;
             Day = dayOfMonth;
         }
         });

     Save.setOnClickListener(SaveOnClickListener);
     Cancel.setOnClickListener(CancelOnClickListener);
        break;
    }
    return dialog;
}  

 private Button.OnClickListener SaveOnClickListener = new Button.OnClickListener(){      
         @Override
         public void onClick(View arg0) {   
             String not = Notification.getText().toString();
             String fre = Freq.getText().toString();

             Toast toast = Toast.makeText(getApplicationContext(), not+":"+fre+":"+Day+"-"+Month+"-"+Year, Toast.LENGTH_SHORT);
              toast.show();
         }          
    };

    private Button.OnClickListener CancelOnClickListener = new Button.OnClickListener(){
         @Override
         public void onClick(View arg0) {
          dismissDialog(CUSTOM_DIALOG_ID);
         }
     };

}

1 个答案:

答案 0 :(得分:3)

首先,你在onCreate()中不需要这一行:

dp=(DatePicker)findViewById(R.id.notdate);

由于你的DatePicker视图不在屏幕上,这将在你的onCreate()中返回null;

在onCreateDialog中我认为你只需要做到:

dp = (DatePicker)dialog.findViewById(R.id.notdate);

那应该修复你的空指针。