当我将一个长变量传递给Thread.Sleep()时,这行不通

时间:2019-03-26 08:22:38

标签: java android android-edittext parseint

这有效:Thread.Sleep(1000)

这不起作用:Thread.Sleep(frequency)

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

1 个答案:

答案 0 :(得分:0)

如果频率属于long类型并且大于1,则1/frequency为零(因为/表示整数除法)。替换为:frequency = 1000/frequency