A build up of lactic acid is one of the possible causes, but it’s not the lactic acid itself that is the cause of cramp.
The biology behind cramps is quite complicated (you’d need a proper medical type person to fully explain it!) Basically though, your muscles rely on calcium being pumped into and out of parts of them (called the ‘lumen’ and ‘sarcoplasmic reticulum’ – basically tubes and membranes which regulate what goes in and out of your muscles). These require water with a very specific concentration of chemicals in it to function correctly. If the concentration changes for any of the reasons that Alex mentioned, then the lumen/reticulum can ‘short circuit’ – leaving calcium stuck in the muscles and preventing them from relaxing.