-3

コード内のいくつかのメソッドから 2 つの変換された日付と時刻を文字列に返して、ユーザーがデータベース内のユーザーの予定時刻とデータを更新できるようにしようとしています。ボタンをクリックして OnClickListner を呼び出すと、null ポインター エラーが発生します。

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)

ここに私のクラス全体、2 つのメソッドが一番下にあります。

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

なので、次の呼び出しをすると、setTimeですnull

calendar.set(Calendar.HOUR_OF_DAY, setTime.getCurrentHour());

を取得してNullPointerExceptionいます。

編集:

例外を読むと、次の 2 行で手がかりが得られます。

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 に答える