Question

The problem I am having is I am trying to receive user input to compare a scheduled date and time that the user inputs and submits via a submit button and a datepicker, timepicker and numberpick (for seconds).

This information is then set into a calendar object when the user presses submit and the app is always looping through the IOIOlooper to check if an event should be called (such as the pin being turned on to turn on a light)...

Any questions, concerns or clarification needed please feel free to tell me.

*EDIT: Also, the app immediately crashes and produces a null pointer error in logcat.

Thanks guys;

Main Activity this activity is the main activity of the program and it is run when the app starts and it is an extension of IOIOActivity for interfacing with IOIO boards:

    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());
        }
    });

Inner class:

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();
}

Tried editting it to this:

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

        }

I changed the calendar to another timer object and am trying to just compare the two values. I instantiated the eventTime object to all 0's.

*3rd Edit: I also removed the conditional statement entirely and it still crashes; I don't think the loop/check statement is what is causing the issue here.

I get a: Java Runtime exception: Unable to start Activity ComponentInfo

Solved it:

I just had to remove the instantiation with all zeros at the top here: eventCalendar.set(0,0,0,0,0,0);

also; the Time.compare(now,eventTime)==0 is the better comparison that works.

Was it helpful?

Solution

if(now.monthDay == eventCalendar.DAY_OF_MONTH) is your problem.

eventCalendar.DAY_OF_MONTH is going to always be equal to 5 because it's a constant. You want to do this: if(now.monthDay == eventCalendar.get(Calendar.DAY_OF_MONTH))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top