我正在开发一个涉及振动的小应用程序,每当调用vibrate()
函数时,应用程序就会崩溃。堆栈跟踪链接回带有文本"Vibrator: Failed to vibrate“的vibrator.vibrate()行。振动器。还有几个DeadObjectExceptions
和RuntimeExceptions
都链接到安卓类。这只会在一些手机上发生,而在其他手机上,它工作得很完美。
hasVibrator()
返回true
,可控震源对象不为空,而且手机有一个可控震源,所以我不知道出了什么问题。也许我试图在创建可控震源对象后过快地振动,或者我在onCreate()
方法中过快地创建了可控震源对象?
下面是我使用vibrate()
方法的那部分代码:
//vibrate only if exists
if (vibrating)
{
long[] pattern = {0, power, target - power};
try
{
vibrator.vibrate(pattern, 0);
}
catch (Exception e)
{
vibrating = false;
}
}
发布于 2017-05-05 09:05:02
假设您使用的是getSystemService,那么您可以期望获得服务,或者如果该类不是受支持的系统服务,则为null。
现在,既然你说你在不同的设备上得到了某些异常,你就可以捕获这些异常。
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if(vibrator != null)
{
try
{
long[] pattern = { 0, 200, 500 };
vibrator.vibrate(pattern, 0);
}
catch(DeadObjectExceptions e)
{
e.printStackTrace();
}
}
https://stackoverflow.com/questions/43794819
复制相似问题