当我将一个长变量传递给Thread.Sleep()时,这不起作用[duplicate]

问题描述 投票:0回答:1

这个问题在这里已有答案:

这有效: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();
            }
        }
}
java android android-edittext parseint
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.