我使用对话框制作了DatePicker。当我从对话框设置日期时,程序崩溃并说“不幸的是prgram已经停止”。这是我的代码:
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
方法中。有人可以帮忙吗?
我的LogCat:
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)
答案 0 :(得分:1)
您的editText
未初始化,是一个空(null)变量。
editText
在哪里可见?
如果您在布局文件中声明了editText
,请使用以下代码获取布局中的editText
:
editText= (EditText) findViewById(R.id.editText);
我假设布局文件中的EditText名为editText
。
答案 1 :(得分:0)
您的edittext未初始化。
您将获得空指针异常,因为您没有初始化edittext。
使用以下内容作为参考。在按钮上单击日期选择器弹出。选择日期并单击设置,日期设置为edittext。
示例:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="55dp"
android:ems="10" >
<requestFocus />
</EditText>
pickerdate类
public class pickerdate extends Activity {
static EditText mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay = (EditText) findViewById(R.id.editText1);
mPickDate = (Button) findViewById(R.id.button1);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
}
public static 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) {
mDateDisplay .setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}
}