android - Vibrator.vibrate() 抛出 ArrayIndexOutOfBoundsException

标签 android exception vibration

我使用以下代码 fragment 以特定模式振动手机,但它抛出 ArrayIndexOutOfBoundsException

vibrator.vibrate(new long[] { selectedDuration, CONSTANT_DELAY }, REPEAT); 

但是

vibrator.vibrate(VIBRATE_DURATION);

工作正常。有什么指点吗?

最佳答案

文档说:

If you want to repeat, pass the index into the pattern at which to start the repeat.

表示在您的情况下 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();
    }
}

关于android - Vibrator.vibrate() 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915382/

相关文章:

android - 如何在 Android Studio 中打开 Gradle 项目(修复 "gradle sync fails error")——打开 zip 文件时出错

java - 我的程序中出现 IllegalStateException?

java - 我尝试调用虚拟方法 'void android.support.v7.widget.AppCompatImageView.setImageDrawable(android.graphics.drawable.Drawable)?

c# - 振动直到消息框关闭 Windows Phone 7

android - 错误输入的振动模式 (Android)

android - Xamarin Forms Android App 一直隐藏操作栏

Android 4.4 KitKat 随机崩溃

Android,更改手机的配置文件

android - 如果在 Android 中不再可见,则 Activity 被系统杀死的问题

objective-c - 调用 CGPDFContextClose 中的 EXC_BAD_ACCESS