我有一个活动有2个按钮 - 一个用于DatePicker和一个TimePicker。当我选择应用程序崩溃的时间并给出空指针异常时。我认为它发生在我的ActivityResult方法中,但不确定,因为我不理解logcat错误。如果我在错误的位置查找错误,我可以提供更多代码。任何帮助都会很棒。 Thansk。
这是logcat:
04-28 12:18:01.676 13968-13968/com.pctoolman.planme.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-28 12:18:01.766 13968-13968/com.pctoolman.planme.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1320)
at android.text.format.DateFormat.format(DateFormat.java:438)
at com.pctoolman.planme.app.NewEventSetupFragment.updateDate(NewEventSetupFragment.java:36)
at com.pctoolman.planme.app.NewEventSetupFragment.onActivityResult(NewEventSetupFragment.java:56)
at com.pctoolman.planme.app.TimePickerFragment.sendResult(TimePickerFragment.java:45)
at com.pctoolman.planme.app.TimePickerFragment.access$100(TimePickerFragment.java:22)
at com.pctoolman.planme.app.TimePickerFragment$2.onClick(TimePickerFragment.java:92)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
04-28 12:18:58.726 13968-13968/com.pctoolman.planme.app I/Process﹕ Sending signal. PID: 13968 SIG: 9
04-28 12:18:59.876 14006-14006/com.pctoolman.planme.app E/Trace﹕ error opening trace file: No such file or directory (2)
这是我的ActivityResult方法。
private static final String DIALOG_DATE = "date";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_TIME = 0;
public void updateDate() {
mEventDateButton.setText(DateFormat.format("EEEE, MMM dd, yyyy", mEvent.getDate()).toString());
}
public void updateTime() {
mEventTimeButton.setText(mEvent.getTime().toString());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEvent = new Event();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) return;;
if (requestCode == REQUEST_DATE) {
Date date = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mEvent.setDate(date);
updateDate();
} else if(requestCode == REQUEST_TIME){
Date time = (Date)data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
mEvent.setTime(time);
updateTime();
}
}
newEventSetupFragment的第36行
public void updateDate() {
mEventDateButton.setText(DateFormat.format("EEEE, MMM dd, yyyy", mEvent.getDate()).toString());
}
event.java 公共课活动{
private UUID mId;
private String mEventNameTitle;
private Date mDate;
private Date mTime;
public Event() {
//generate uniques identifier
mId=UUID.randomUUID();
//generate date
mDate = new Date();
//generate time
mTime = new Date();
}
public UUID getId() {
return mId;
}
//event name title getter and setter
public String getTitle() {
return mEventNameTitle;
}
public void setTitle(String title){
mEventNameTitle = title;
}
//event date getter and setter
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
//event time getter and setter
public Date getTime() {
return mTime;
}
public void setTime (Date time) {
mTime = time;
}
}
mEventDate和mEventTime按钮
event date button set
mEventDateButton = (Button)v.findViewById(R.id.eventDateButton);
updateDate();
mEventDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm =getActivity().getSupportFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mEvent.getDate());
dialog.setTargetFragment(NewEventSetupFragment.this, REQUEST_DATE);
dialog.show(fm, DIALOG_DATE);
}
});
//event time button set
mEventTimeButton = (Button)v.findViewById(R.id.eventTimeButton);
updateTime();
mEventTimeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
TimePickerFragment dialog = TimePickerFragment.newInstance(mEvent.getTime());
dialog.setTargetFragment(NewEventSetupFragment.this,REQUEST_TIME);
dialog.show(fm, DIALOG_DATE);