这个问题在这里已有答案:
这有效:Thread.Sleep(1000)
这不起作用:Thread.Sleep(frequency)
频率变量从用户获取edittext
输入并将其解析为整数,因此它应该正常传递。我不知道我做错了什么。帮助赞赏:)
private void FlashButtonClicked() {
startPattern = true;
try{
frequency = Long.parseLong(frequencyInput.getText().toString());
}catch(NumberFormatException e){
Toast.makeText(getBaseContext(), "value not acceptable", Toast.LENGTH_LONG).show();
}
frequency = (1 / frequency) * 1000;
while(startPattern){
enableTorch();
try{
// see if you can figure out why I can't pass the user input into the
//Thread.sleep() method without it fucking up. When you change it manually with
// a long input, it works just fine.
Thread.sleep(frequency);
}catch(Exception e){
e.printStackTrace();
}
}
}
如果频率是long
类型且大于1,则1/frequency
为零(因为/
表示整数除法)。替换为:frequency = 1000/frequency