【问题标题】:Using DatePicker on custom dialog在自定义对话框中使用 DatePicker
【发布时间】:2011-12-24 00:52:14
【问题描述】:

嗯,这就是我的代码。我想从自定义对话框中的 DatePicker 中获取值。当我尝试初始化 dp (DatePicker) 时出现 nullPointerException 错误,我无法将 DatePicker 中的值转换为变量。 :/ 有人知道发生了什么吗? (我有 1 个布局。日期选择器的主要(R.layout.notifications)和对话框布局(R.layout.notifications_dialog)位于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);
         }
     };

}

【问题讨论】:

    标签: android datepicker


    【解决方案1】:

    对于初学者,您的 onCreate() 中不需要此行:

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

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

    在 onCreateDialog 里面我想你只需要做到:

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

    这应该可以修复你的空指针。

    【讨论】:

    • 哦,是的!那是正确的!对话框内的上述声明是这样的。我是多么愚蠢:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多