-3

我试图从我的代码中的几个方法将两个转换后的日期和时间返回为字符串,以允许用户更新我的数据库中的用户约会时间和数据。当我单击按钮并调用 OnClickListner 时,出现空指针错误:

日志猫:

 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;
    }
}
4

1 回答 1

3
TimePicker setTime;

看来您还没有为变量分配任何TimePicker实例。setTime

因此,当您执行以下呼叫时,setTimenull.

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 calendarvariable is not null,因此唯一的可能性是setTime可变的。

于 2013-02-07T21:44:28.520 回答