Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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]如何在片段中设置AlertDialog的最终值_Android_Android Fragments - Fatal编程技术网

[Android]如何在片段中设置AlertDialog的最终值

[Android]如何在片段中设置AlertDialog的最终值,android,android-fragments,Android,Android Fragments,我想使用AlertDialog在我触摸片段中的编辑文本时显示日历。在过去,当我使用活动课时,它工作得非常好。但当我使用Fragment时,它不起作用 以下是触摸“编辑片段中的文本”时显示日历对话框的方法: private void setDateTimeField() { try { et_date.setOnClickListener(this); final Calendar newCalendar = Calendar.getInstance()

我想使用AlertDialog在我触摸片段中的编辑文本时显示日历。在过去,当我使用活动课时,它工作得非常好。但当我使用Fragment时,它不起作用

以下是触摸“编辑片段中的文本”时显示日历对话框的方法:

 private void setDateTimeField() {
    try {
        et_date.setOnClickListener(this);

        final Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth - 1);
                if (newDate.getTime().getTime() > (newCalendar.getTime().getTime())) {

                    final  AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();

                    //this code below is coppied in https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
                    final Timer t = new Timer();
                    t.schedule(new TimerTask() {
                        public void run() {
                            builder.dismiss(); // when the task active then close the dialog
                            t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                        }
                    }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
                    et_date.setText(dateFormatter.format(newCalendar.getTime()));

                } else {
                    newDate.set(year, monthOfYear, dayOfMonth);
                    et_date.setText(dateFormatter.format(newDate.getTime()));
                }
            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    } catch (Exception ex) {
        messages("Something Wrong!");
    }
}
这是错误的:

 final  AlertDialog builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();
当我删除final for builder时,上述代码正常,但其他问题出现在:

builder.dismiss();
上述代码要求生成器必须是最终的。我知道解决我问题的最好办法是使用DialogFragment。但我不想用它。此外,我想知道这个错误的原因。请帮帮我。 我的英语不好,所以如果我的语法有任何错误,我很抱歉,谢谢你阅读我的问题。

fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

改变


这听起来可能很奇怪,但你忘了创建AlertDialog吗?@Reiz这是在这段代码上创建的。很抱歉,这不是我的问题。如果我在Avtivity类上使用这段代码,没什么错。这听起来可能很奇怪,但你忘了创建AlertDialog吗?@Reiz这是在这段代码上创建的。很抱歉,这不是我的问题。如果我在Avtivity类上使用此代码,没有问题。谢谢你,兄弟,我现在将尝试它。它显示相同的错误:。无法在Disclose中添加final。这不是一个隐藏错误,当我在builder或dissmiss中添加final时,android studio显示onDateSet方法中的所有代码都是错误的。如果我删除final,如果我将final放在AlertDialog,android studio会在dissmis中显示错误,反之亦然。我会为您拍照,请稍等几分钟。我应该更早意识到这一点,因为您提到它在活动中工作,但在片段中不工作。现在你也看到了添加详细错误的重要性:谢谢兄弟,我现在就试试。它显示了相同的错误:。无法在Disclose中添加final。这不是一个隐藏错误,当我在builder或dissmiss中添加final时,android studio显示onDateSet方法中的所有代码都是错误的。如果我删除final,如果我将final放在AlertDialog,android studio会在dissmis中显示错误,反之亦然。我会为您拍照,请稍等几分钟。我应该更早意识到这一点,因为您提到它在活动中工作,但在片段中不工作。现在您还可以看到添加详细错误的重要性:
fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {