Using DatePicker on custom dialog

微笑、不失礼 提交于 2019-12-11 11:01:43

问题


Well ,thats my code.I want to take values from a DatePicker from a custom Dialog. When i try to initialize dp (DatePicker) i got a nullPointerException error and i cant take the values from DatePicker to variables. :/ Anyone knows what is going on? (I have 1 layouts. The main (R.layout.notifications) and the dialog layout (R.layout.notifications_dialog) the datepicker is on the notifications_dialog layout.

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:


For starters, you don't need this line in your onCreate():

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

Since your DatePicker View is not on the screen yet this will return null to you inside your onCreate();

Inside the onCreateDialog I think you just need to make it:

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

That should fix your null pointer.



来源:https://stackoverflow.com/questions/8112534/using-datepicker-on-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!