我试图将两个转换后的日期和时间返回到我的代码中的几个方法的字符串,以允许用户更新用户在我的数据库中的约会时间和数据。当我点击我的按钮并调用我的OnClickListner时,我得到一个空指针错误:
logcat的:
02-07 21:26:04.869: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-07 21:26:04.949: E/AndroidRuntime(305): FATAL EXCEPTION: main
02-07 21:26:04.949: E/AndroidRuntime(305): java.lang.NullPointerException
02-07 21:26:04.949: E/AndroidRuntime(305): at com.example.flybase2.AppointmentEditChanges.setTime(AppointmentEditChanges.java:278)
02-07 21:26:04.949: E/AndroidRuntime(305): at com.example.flybase2.AppointmentEditChanges.onClick(AppointmentEditChanges.java:304)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.view.View.performClick(View.java:2408)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.view.View$PerformClick.run(View.java:8816)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.os.Handler.handleCallback(Handler.java:587)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
02-07 21:26:04.949: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 21:26:04.949: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 21:26:04.949: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
02-07 21:26:04.949: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 21:26:04.949: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 21:26:04.949: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
这是我的全班,两种方法都在底部,希望有人能看到我出错的地方!:
package com.example.flybase2;
public class AppointmentEditChanges extends Activity implements OnClickListener {
String dateToSetA;
String timeToSetA;
String dateToSetB;
String timeToSetB;
Date setDate;
Long passedID;
EditText editAppointName;
Spinner editTime;
TimePicker editTimePicker;
DatePicker editDatePicker;
Date dateToEdit;
Date cake;
Date dateToEditApp;
EditText editCommentsApp;
ToggleButton editToggle;
boolean setTog;
Button editUpdateApp;
Button editDelApp;
String updateNameAppoint;
String updateCommsAppoint;
TimePicker setTime;
DatePicker setDateb;
String typeToSet;
boolean check;
Boolean alarmToSet;
String dateToSet;
String timeToSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editappointment);
editAppointName = (EditText) findViewById(R.id.inputNewAppName);
editTime = (Spinner) findViewById(R.id.spinner1);
editTimePicker = (TimePicker) findViewById(R.id.timePickerEdit);
editDatePicker = (DatePicker) findViewById(R.id.datePickerEdit);
editCommentsApp = (EditText) findViewById(R.id.inputNewComm);
editToggle = (ToggleButton) findViewById(R.id.toggleButton1);
editUpdateApp = (Button) findViewById(R.id.btnChangesApp);
editDelApp = (Button) findViewById(R.id.btnDeleteApp);
Bundle extras = getIntent().getExtras();
if (extras != null) {
passedID = extras.getLong("passedID");
}
DBHandlerApp getName = new DBHandlerApp(this, null, null);
getName.open();
String nameReturned = getName.getNameEditForAppointment(passedID);
String typeReturned = getName.getAppointTypeEditForAppointment(passedID);
getName.close();
editAppointName.setText(nameReturned);
List<String> list = new ArrayList<String>();
list.add("-");
list.add("Medical");
list.add("Business");
list.add("Family");
list.add("Other");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
editTime.setAdapter(dataAdapter);
for(int a = 0; a < list.size(); a ++) {
if (typeReturned.equals(list.get(a))) {
editTime.setSelection(a);
}
}
DBHandlerApp timeToEdit= new DBHandlerApp(this, null, null);
timeToEdit.open();
String returnedTime = timeToEdit.getTime(passedID);
timeToEdit.close();
SimpleDateFormat newFormat = new SimpleDateFormat("HH:mm");
try {
cake = newFormat.parse(returnedTime);
}
catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(cake);
editTimePicker.setCurrentHour(cal.get( Calendar.HOUR_OF_DAY ));
editTimePicker.setCurrentMinute(cal.get( Calendar.MINUTE ));
DBHandlerApp dateToEdit= new DBHandlerApp(this, null, null);
dateToEdit.open();
String returnedDate = dateToEdit.dateToEdit(passedID);
dateToEdit.close();
SimpleDateFormat newFormatDate = new SimpleDateFormat("dd-MMM-yyyy");
try {
dateToEditApp = newFormatDate.parse(returnedDate);
}
catch (ParseException e) {
e.printStackTrace();
}
Calendar calDate = Calendar.getInstance();
calDate.setTime(dateToEditApp);
int year = calDate.get(Calendar.YEAR);
int monthOfYear = calDate.get(Calendar.MONTH);
int dayOfMonth = calDate.get(Calendar.DAY_OF_MONTH);
editDatePicker.init(year, monthOfYear, dayOfMonth, null);
DBHandlerApp commentsApp = new DBHandlerApp(this, null, null);
commentsApp.open();
String returnedComment = commentsApp.getComments(passedID);
commentsApp.close();
editCommentsApp.setText(returnedComment);
typeToSet = editTime.getSelectedItem().toString();
DBHandlerApp editAlarm = new DBHandlerApp(this, null, null);
editAlarm.open();
Integer alarmEditResult = editAlarm.checkAlarmStatus(passedID);
editAlarm.close();
if (alarmEditResult == 1) {
setTog = true;
editToggle.setChecked(setTog);
}
else {
setTog = false;
editToggle.setChecked(setTog);
}
editToggle.setChecked(setTog);
editToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (editToggle.isChecked()) {
setTog = true;
editToggle.setChecked(setTog);
}
else {
setTog = false;
editToggle.setChecked(setTog);
}
}
});
editUpdateApp.setOnClickListener(this);
editDelApp.setOnClickListener(this);
}
@Override
public void onClick(View editChoice) {
switch(editChoice.getId()){
case R.id.btnChangesApp:
updateNameAppoint = editAppointName.getText().toString();
updateCommsAppoint = editCommentsApp.getText().toString();
dateToSetA = setDate();
timeToSetA = setTime();
if(setTog != false) {
try{
alarmToSet = true;
DBHandlerApp updateApp = new DBHandlerApp(this, null, null);
updateApp.open();
updateApp.updateAppDetails(passedID, updateNameAppoint, typeToSet, timeToSetA, dateToSetA, updateCommsAppoint, alarmToSet);
updateApp.close();
//AlarmManager dateSet = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//Intent intent = new Intent(this, DateAlarm.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//dateSet.set(AlarmManager.RTC_WAKEUP, setDate.getTime(), pendingIntent);
}
catch (Exception e) {
check = false;
Dialog d = new Dialog(this);
d.setTitle("Appointment Failed To Be Updated 1");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
finally {
if(check) {
Dialog e = new Dialog(this);
e.setTitle("Appointment Has Been Updated.");
TextView txt = new TextView(this);
txt.setText("Success");
e.setContentView(txt);
e.show();
}
}
}
else if(setTog == false) {
alarmToSet = false;
updateNameAppoint = editAppointName.getText().toString();
updateCommsAppoint = editCommentsApp.getText().toString();
dateToSetB = setDate();
timeToSetB = setTime();
alarmToSet = false;
try {
DBHandlerApp updateApp = new DBHandlerApp(this, null, null);
updateApp.open();
updateApp.updateAppDetails(passedID, updateNameAppoint, typeToSet, timeToSetB, dateToSetB, updateCommsAppoint, alarmToSet);
updateApp.close();
}
catch (Exception e) {
check = false;
Dialog d = new Dialog(this);
d.setTitle("Appointment Failed To Be Added 2");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
finally {
if(check) {
Dialog e = new Dialog(this);
e.setTitle("New Appointment Added.");
TextView txt = new TextView(this);
txt.setText("Success");
e.setContentView(txt);
e.show();
}
finish();
}
}
break;
case R.id.btnDeleteApp:
DBHandlerApp deleteApp = new DBHandlerApp(this, null, null);
deleteApp.open();
deleteApp.deleteAppointment(passedID);
deleteApp.close();
break;
}
}
public String setDate() {
Format formatterDate;
Calendar calendarDate = Calendar.getInstance();
calendarDate.set(Calendar.YEAR, editDatePicker.getYear());
calendarDate.set(Calendar.MONTH, editDatePicker.getMonth());
calendarDate.set(Calendar.DAY_OF_MONTH, editDatePicker.getDayOfMonth());
formatterDate = new SimpleDateFormat("dd-MMM-yyyy");
dateToSet = formatterDate.format(calendarDate.getTime());
return dateToSet;
}
public String setTime() {
Format formatter;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, setTime.getCurrentHour());
calendar.set(Calendar.MINUTE, setTime.getCurrentMinute());
formatter = new SimpleDateFormat("HH:mm");
timeToSet = formatter.format(calendar.getTime());
return timeToSet;
}
}
答案 0 :(得分:3)
TimePicker setTime;
您似乎没有将任何TimePicker
个实例分配给setTime
变量。
因此,当您执行以下呼叫时,setTime
为null
。
calendar.set(Calendar.HOUR_OF_DAY, setTime.getCurrentHour());
您将获得NullPointerException
。
修改强>
如果你读异常,两行给你线索:
com.example.flybase2.AppointmentEditChanges.setTime(AppointmentEditChanges.java:278)
上面一行告诉AppointmentEditChanges.java
第278行是问题而setTime(...
告诉这是setTime
方法。
02-07 21:26:04.949: E/AndroidRuntime(305): java.lang.NullPointerException
由于NullPointerException
导致您的申请失败。
当您在指定行读取源时,您可以轻松识别以下代码
calendar.set(Calendar.HOUR_OF_DAY,setTime.getCurrentHour());
从上面,您调用tell calendar
变量不是null
,因此只有setTime
变量可能。