将日历参数与android时间参数进行比较时为空指针

时间:2014-01-28 18:05:31

标签: java android timer schedule ioio

我遇到的问题是我正在尝试接收用户输入,以比较用户通过提交按钮和datepicker,timepicker和numberpick(秒)提交和提交的预定日期和时间。

当用户按下提交时,该信息将被设置到日历对象中,并且应用程序始终循环通过IOIOlooper以检查是否应该调用事件(例如,打开引脚以打开灯)。

如有任何问题,疑虑或需要澄清,请随时告诉我。

*编辑:此外,应用程序立即崩溃并在logcat中产生空指针错误。

谢谢你们;

主要活动此活动是程序的主要活动,它在应用程序启动时运行,它是IOIOActivity的扩展,用于连接IOIO板:

    now = new Time();
    eventCalendar = eventCalendar.getInstance();
    eventCalendar.set(0,0,0,0,0,0);
    submitEvent = (Button) findViewById(R.id.submit);


    // Wakelock
    PowerManager powerManageDevice = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLockDevice = powerManageDevice.newWakeLock(
            PowerManager.FULL_WAKE_LOCK, "Wakelocking the Scheduler");
    super.onCreate(savedInstanceState);
    wakeLockDevice.acquire();
    setContentView(R.layout.activity_main);

    chooseDay = (DatePicker) findViewById(R.id.datepicker1);
    chooseTime = (TimePicker) findViewById(R.id.timepicker1);

    togglePin = (ToggleButton) findViewById(R.id.ledtoggle);
    chosePin1 = (NumberPicker) findViewById(R.id.pinpick1);
    chosePin1.setMaxValue(46);
    chosePin1.setMinValue(1);

    chosePin2 = (NumberPicker) findViewById(R.id.pinpick2);
    chosePin2.setMaxValue(46);
    chosePin2.setMinValue(1);

    chosePin3 = (NumberPicker) findViewById(R.id.pinpick3);
    chosePin3.setMaxValue(46);
    chosePin3.setMinValue(1);

    chosePin4 = (NumberPicker) findViewById(R.id.pinpick4);
    chosePin4.setMaxValue(46);
    chosePin4.setMinValue(1);

    chosePin5 = (NumberPicker) findViewById(R.id.pinpick5);
    chosePin5.setMaxValue(46);
    chosePin5.setMinValue(1);

    chosePin6 = (NumberPicker) findViewById(R.id.pinpick6);
    chosePin6.setMaxValue(46);
    chosePin6.setMinValue(1);

    chosePin7 = (NumberPicker) findViewById(R.id.pinpick7);
    chosePin7.setMaxValue(46);
    chosePin7.setMinValue(1);

    chosePin8 = (NumberPicker) findViewById(R.id.pinpick8);
    chosePin8.setMaxValue(46);
    chosePin8.setMinValue(1);

    chosePin9 = (NumberPicker) findViewById(R.id.pinpick9);
    chosePin9.setMaxValue(46);
    chosePin9.setMinValue(1);

    secondPicker = (NumberPicker) findViewById(R.id.secondPicker);
    secondPicker.setMaxValue(59);
    secondPicker.setMinValue(0);

    submitEvent.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            eventCalendar.set(chooseDay.getYear(), chooseDay.getMonth(), chooseDay.getDayOfMonth(), chooseTime.getCurrentHour(), chooseTime.getCurrentMinute(), secondPicker.getValue());
        }
    });

内班:

class Looper extends BaseIOIOLooper {
    /** The on-board LED. */
    private DigitalOutput led_;
    private DigitalOutput pin1PWM;
    private DigitalOutput pin2PWM;
    private DigitalOutput pin3PWM;
    private DigitalOutput pin4PWM;
    private DigitalOutput pin5PWM;
    private DigitalOutput pin6PWM;
    private DigitalOutput pin7PWM;
    private DigitalOutput pin10PWM;
    private DigitalOutput pin11PWM;
    private DigitalOutput pin12PWM;
    private DigitalOutput pin13PWM;
    private DigitalOutput pin14PWM;
    private DigitalOutput pin27PWM;
    private DigitalOutput pin28PWM;
    private DigitalOutput pin34PWM;
    private DigitalOutput pin35PWM;
    private DigitalOutput pin36PWM;
    private DigitalOutput pin37PWM;
    private DigitalOutput pin38PWM;
    private DigitalOutput pin39PWM;
    private DigitalOutput pin40PWM;

    /**
     * private DigitalOutput pin1; private DigitalOutput pin2; private
     * DigitalOutput pin3; private DigitalOutput pin4; private DigitalOutput
     * pin5; private DigitalOutput pin6; private DigitalOutput pin7;
     */
    private DigitalOutput pin8;
    private DigitalOutput pin9;
    /**
     * private DigitalOutput pin10; private DigitalOutput pin11; private
     * DigitalOutput pin12; private DigitalOutput pin13; private
     * DigitalOutput pin14;
     */
    private DigitalOutput pin15;
    private DigitalOutput pin16;
    private DigitalOutput pin17;
    private DigitalOutput pin18;
    private DigitalOutput pin19;
    private DigitalOutput pin20;
    private DigitalOutput pin21;
    private DigitalOutput pin22;
    private DigitalOutput pin23;
    private DigitalOutput pin24;
    private DigitalOutput pin25;
    private DigitalOutput pin26;
    // private DigitalOutput pin27;
    // private DigitalOutput pin28;
    private DigitalOutput pin29;
    private DigitalOutput pin30;
    private DigitalOutput pin31;
    private DigitalOutput pin32;
    private DigitalOutput pin33;
    /**
     * private DigitalOutput pin34; private DigitalOutput pin35; private
     * DigitalOutput pin36; private DigitalOutput pin37; private
     * DigitalOutput pin38; private DigitalOutput pin39; private
     * DigitalOutput pin40;
     */
    private DigitalOutput pin41;
    private DigitalOutput pin42;
    private DigitalOutput pin43;
    private DigitalOutput pin44;
    private DigitalOutput pin45;
    private DigitalOutput pin46;


    private DigitalOutput[] pinDigArray = { pin1PWM, pin2PWM, pin3PWM,
            pin4PWM, pin5PWM, pin6PWM, pin7PWM, pin8, pin9, pin10PWM,
            pin11PWM, pin12PWM, pin13PWM, pin14PWM, pin15, pin16, pin17,
            pin18, pin19, pin20, pin21, pin22, pin23, pin24, pin25, pin26,
            pin27PWM, pin28PWM, pin29, pin30, pin31, pin32, pin33,
            pin34PWM, pin35PWM, pin36PWM, pin37PWM, pin38PWM, pin39PWM,
            pin40PWM, pin41, pin42, pin43, pin44, pin45, pin46 };

    /**
     * Called every time a connection with IOIO has been established.
     * Typically used to open pins.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
     */
    @Override
    protected void setup() throws ConnectionLostException {
        led_ = ioio_.openDigitalOutput(0, true);

        for (int i = 0; i < pinDigArray.length; i++) {
            pinDigArray[i] = ioio_.openDigitalOutput(i + 1, false);
        }
    }

    /**
     * Called repetitively while the IOIO is connected.
     * 
     * @throws ConnectionLostException
     *             When IOIO connection is lost.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
     */
    @Override
    public void loop() throws ConnectionLostException {
        now.setToNow();


        led_.write(!togglePin.isChecked());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }

        for (int i = 0; i < pinDigArray.length; i++) {

            if (chosePin1.getValue() == i + 1) {
                if(now.monthDay == eventCalendar.DAY_OF_MONTH)
                pinDigArray[i].write(true);
            } else {
                pinDigArray[i].write(false);
            }

        }
    }
}

/**
 * A method to create our IOIO thread.
 * 
 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
 */
@Override
protected IOIOLooper createIOIOLooper() {
    return new Looper();
}

尝试编辑它:

public void loop() throws ConnectionLostException {
        now.setToNow();

        led_.write(!togglePin.isChecked());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }

        for (int i = 0; i < pinDigArray.length; i++) {

            if (chosePin1.getValue() == i + 1) {
                if(now.compare(now, eventTime) == 0)

                pinDigArray[i].write(true);
            } else {
                pinDigArray[i].write(false);
            }

        }

我将日历更改为另一个计时器对象,我试图只比较这两个值。我将eventTime对象实例化为全0。

* 3rd Edit:我也完全删除了条件语句,它仍然崩溃;我不认为循环/检查语句是导致此问题的原因。

我得到:Java Runtime异常:无法启动Activity ComponentInfo

解决了它:

我只需要删除顶部全部为零的实例化: eventCalendar.set(0,0,0,0,0,0);

也; Time.compare(现在,eventTime)== 0是更好的比较。

1 个答案:

答案 0 :(得分:0)

if(now.monthDay == eventCalendar.DAY_OF_MONTH)是你的问题。

eventCalendar.DAY_OF_MONTH将始终等于5,因为它是常量。你想这样做:if(now.monthDay == eventCalendar.get(Calendar.DAY_OF_MONTH))