在自定义对话框中使用的DatePicker [英] Using DatePicker on custom dialog

查看:143
本文介绍了在自定义对话框中使用的DatePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这就是我的code.I想从一个自定义对话框一个datepicker取值。当我尝试初始化DP(的DatePicker)我得到NullPointerException错误,我不能把值从的DatePicker变量。 :/任何人都知道是怎么回事? (我有1布局,主要(R.layout.notifications)和对话框布局(R.layout.notifications_dialog)的日期选择器是在notifications_dialog布局。

 公共类通知扩展活动{静态最终诠释CUSTOM_DIALOG_ID = 0;
通知的EditText,频率;
按钮保存,取消;
INT年,月,日;
DatePicker的DP;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.notifications);
    OK按钮=(按钮)findViewById(R.id.ok);
    DP =(的DatePicker)findViewById(R.id.notdate);
    ok.setOnClickListener(新OnClickListener(){
             公共无效的onClick(查看ovuinfo){
                的ShowDialog(CUSTOM_DIALOG_ID);
               }
           });
}@覆盖
保护对话框onCreateDialog(INT ID){
 对话的对话= NULL ;;
    开关(ID){
    案例CUSTOM_DIALOG_ID:
     对话框=新的对话框(Notifications.this);     dialog.setContentView(R.layout.notifications_dialog);
     dialog.setTitle(添加通知);     通知=(EditText上)dialog.findViewById(R.id.notification);
     频率=(EditText上)dialog.findViewById(R.id.freq);
     保存=(按钮)dialog.findViewById(R.id.add);
     取消=(按钮)dialog.findViewById(R.id.canc);
     DP =(的DatePicker)findViewById(R.id.notdate);     最后的日历CAL = Calendar.getInstance();
     年整型= cal.get(Calendar.YEAR);
     INT monthOfYear = cal.get(的Calendar.MONTH);
     INT请将dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);      //这里是问题!
      dp.init(年,monthOfYear,请将dayOfMonth,新OnDateChangedListener(){
         @覆盖
         公共无效onDateChanged(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth){
             年=年;
             月= monthOfYear;
             天=请将dayOfMonth;
         }
         });     Save.setOnClickListener(SaveOnClickListener);
     Cancel.setOnClickListener(CancelOnClickListener);
        打破;
    }
    返回对话框;
} 私人Button.OnClickListener SaveOnClickListener =新Button.OnClickListener(){
         @覆盖
         公共无效的onClick(查看为arg0){
             。字符串不等于Notification.getText()的toString();
             。字符串FRE = Freq.getText()的toString();             吐司面包= Toast.makeText(getApplicationContext(),而不是+:+ FRE +:+日+ - +月+ - +年,Toast.LENGTH_SHORT);
              toast.show();
         }
    };    私人Button.OnClickListener CancelOnClickListener =新Button.OnClickListener(){
         @覆盖
         公共无效的onClick(查看为arg0){
          dismissDialog(CUSTOM_DIALOG_ID);
         }
     };}


解决方案

对于初学者来说,你不需要在你的onCreate()这一行:

  DP =(的DatePicker)findViewById(R.id.notdate);

由于您的DatePicker查看是不是在屏幕上但这种将返回null给你你的onCreate()内;

在onCreateDialog里面我觉得你只需要使它:

  DP =(的DatePicker)dialog.findViewById(R.id.notdate);

这应该解决您的空指针。

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);
         }
     };

}

解决方案

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.

这篇关于在自定义对话框中使用的DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆