Vibrator.vibrate()抛出ArrayIndexOutOfBoundsException

Rag*_*har 2 android exception vibration

我使用以下代码片段以特定模式振动手机,但它会抛出和ArrayIndexOutOfBoundsException.

vibrator.vibrate(new long[] { selectedDuration, CONSTANT_DELAY }, REPEAT); 
Run Code Online (Sandbox Code Playgroud)

vibrator.vibrate(VIBRATE_DURATION);
Run Code Online (Sandbox Code Playgroud)

工作良好.有什么指针吗?

sst*_*stn 10

文档说:

如果要重复,请将索引传递到开始重复的模式.

意味着在您的情况下,REPEAT只允许为0或1.

这是实施:

public void vibrate(long[] pattern, int repeat)
{
    // catch this here because the server will do nothing.  pattern may
    // not be null, let that be checked, because the server will drop it
    // anyway
    if (repeat < pattern.length) {
        try {
            mService.vibratePattern(pattern, repeat, mToken);
        } catch (RemoteException e) {
        }
    } else {
        throw new ArrayIndexOutOfBoundsException();
    }
}
Run Code Online (Sandbox Code Playgroud)