Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-自定义对话框中的日期选择器_Android_Android Fragments_Datepicker_Android Dialog_Android Datepicker - Fatal编程技术网

Android-自定义对话框中的日期选择器

Android-自定义对话框中的日期选择器,android,android-fragments,datepicker,android-dialog,android-datepicker,Android,Android Fragments,Datepicker,Android Dialog,Android Datepicker,我得到了一个自定义对话框,上面有一个日期选择器。在我的Fragment上,我得到一个按钮,当我按下它时,对话框显示出来,我从日期选择器中选择日期。我希望所选日期显示在我的片段的文本视图中。我的代码如下: 这是我的主活动上包含片段的对话框的代码: public int day; public int month; public int year; @SuppressLint("InflateParams") @SuppressWarnings("deprecati

我得到了一个自定义对话框,上面有一个日期选择器。在我的
Fragment
上,我得到一个按钮,当我按下它时,对话框显示出来,我从
日期选择器中选择日期。我希望所选日期显示在我的
片段的
文本视图中。我的代码如下:

这是我的主
活动
上包含
片段的对话框的代码

public int day;
    public int month;
    public int year;

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    public void SetDate(){
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Select Date");
        alertDialog.setIcon(R.drawable.action_reservations);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View promptView = layoutInflater.inflate(R.layout.datepicker, null);
        alertDialog.setView(promptView);
        final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
        final TextView PickedDate = (TextView) findViewById(R.id.pickeddate);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                day=datePicker.getDayOfMonth();
                month=datePicker.getMonth() + 1;
                year=datePicker.getYear();
                PickedDate.setText(new StringBuilder().append(day).append(" ").
                        append("-").append(month).append("-").append(year));
            }

        });
        // Showing Alert Message
        alertDialog.show();
       }
我从日期选择器中获取日期、月份和年份,并使用

PickedDate.setText(new StringBuilder().append(day).append(" ").
                            append("-").append(month).append("-").append(year));
同样在我的片段中,我调用我的对话框,代码如下:

final Button SetDate = (Button)rootView.findViewById(R.id.selectdate);
        SetDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {       
                ((MainActivity) getActivity()).SetDate();            
            }  
            });
当我运行我的应用程序时,我在以下几行中得到一个nullPointerException:

day=datePicker.getDayOfMonth();
month=datePicker.getMonth() + 1;
year=datePicker.getYear();
我做错了什么?提前感谢

尝试更换

final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
final TextView PickedDate = (TextView) findViewById(R.id.pickeddate);
用这个

final DatePicker datePicker = (DatePicker) promptView.findViewById(R.id.datePicker1);
final TextView PickedDate = (TextView) promptView.findViewById(R.id.pickeddate);

我在
pickeDate.setText(new StringBuilder().append(day).append(“”).append(“”).append(“”.append(“”).append(month.append(“”).append(“”.append(year))中出错我刚刚找到它,我不必在TextView中使用promtView。代码是final DatePicker DatePicker=(DatePicker)promptView.findviewbyd(R.id.datePicker1);最终TextView PickedDate=(TextView)findViewById(R.id.PickedDate);谢谢