我遇到的问题是我试图接收用户输入来比较用户通过提交按钮和日期选择器、时间选择器和数字选择器输入和提交的预定日期和时间(秒)。
然后,当用户按下提交时,此信息将设置到日历对象中,并且应用程序始终循环通过 IOIOlooper 以检查是否应调用事件(例如打开引脚以打开灯)...
如有任何问题、疑虑或需要澄清,请随时告诉我。
*编辑:此外,应用程序立即崩溃并在 logcat 中产生空指针错误。
多谢你们;
Main Activity 这个activity是程序的主要activity,它在应用程序启动时运行,它是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。
*第三次编辑:我还完全删除了条件语句,但它仍然崩溃;我不认为循环/检查语句是导致问题的原因。
我得到一个:Java 运行时异常:无法启动 Activity ComponentInfo
解决了它:
我只需要在此处删除顶部全为零的实例: eventCalendar.set(0,0,0,0,0,0);
还; Time.compare(now,eventTime)==0 是更好的比较。