Probably the class which you are calling that isn't an Activity
or some other class which has it's own context. So if you are calling that in function you should do something like this :
public void someFunction(Context context){
// .. code
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
// .. more code
}
or in class
public class MyClass {
private Context mContext;
public MyClass(Context context){
mContext = context;
}
public void someFunction(){
// code
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
// .. code
}
}
and as the other answers mentioned, don't forget to add permission to your manifest file :
<uses-permission android:name="android.permission.VIBRATE"/>
Hope this helps! : )