gpt4 book ai didi

android - 在 EditText 中设置日期时出错

转载 作者:行者123 更新时间:2023-11-30 03:33:53 25 4
gpt4 key购买 nike

我制作了带对话框的 DatePicker。当我从对话框中设置日期时,我的程序崩溃并显示“不幸的是程序已停止”。这是我的代码:

package com.example.reminder;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;


public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

public EditText editText;
DatePicker dpResult;

public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {

editText.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also


}
}

我知道问题出在 onDateSet 方法中。有人可以帮忙吗?

我的日志:

06-05 12:19:40.702: E/AndroidRuntime(2876): FATAL EXCEPTION: main
06-05 12:19:40.702: E/AndroidRuntime(2876): java.lang.NullPointerException
06-05 12:19:40.702: E/AndroidRuntime(2876): at com.example.reminder.DatePickerFragment.onDateSet(DatePickerFragment.java:38)
06-05 12:19:40.702: E/AndroidRuntime(2876): at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
06-05 12:19:40.702: E/AndroidRuntime(2876): at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
06-05 12:19:40.702: E/AndroidRuntime(2876): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-05 12:19:40.702: E/AndroidRuntime(2876): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 12:19:40.702: E/AndroidRuntime(2876): at android.os.Looper.loop(Looper.java:137)
06-05 12:19:40.702: E/AndroidRuntime(2876): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-05 12:19:40.702: E/AndroidRuntime(2876): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 12:19:40.702: E/AndroidRuntime(2876): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 12:19:40.702: E/AndroidRuntime(2876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-05 12:19:40.702: E/AndroidRuntime(2876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-05 12:19:40.702: E/AndroidRuntime(2876): at dalvik.system.NativeStart.main(Native Method)
06-05 12:19:44.152: E/Trace(2899): error opening trace file: No such file or directory (2)

最佳答案

您的 editText 未初始化,是一个空 (null) 变量。

editText 在哪里可见?

如果您在布局文件中声明了 editText,请使用以下代码在布局中获取 editText:

editText= (EditText) findViewById(R.id.editText);

我假设您的布局文件中的 EditText 名为 editText

关于android - 在 EditText 中设置日期时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com