I started to get these pains in college, and always first thing in the morning or while sleeping. Like always, I would stretch and it would bring on a painful cramp in my leg or foot. I would lie there in agony massaging the muscle until the spasm went away. The problem with this method of relief is that it didn't work well, and the cramp would last for minutes. Sometimes it was so bad that my muscles would be sore for days. I was a sad rabbit, because it seemed like nothing could be done to stop them.
One day it happened again, and for some reason I just thought to stand up. This stopped the pain instantly! Not even a few seconds passed; I mean instantly and I was amazed. So whenever I get them, I stand up quickly and walk around. It always works, because what I think happens is that if the leg/feet muscles have to be used for walking they're forced to flex and stretch, therefore stopping the cramp.
Now prevention is another thing completely. To be honest, I don't know what causes these cramps. I've heard three theories about it though. One, is that they're caused by wearing shoes, specifically high heels. This can't be though, because I haven't worn shoes or socks in over a week (Yes, I didn't go anywhere at all and I've been exercising inside). My feet are spoiled as can be. The second, is a potassium and/or a vitamin e deficiency and this can't be either because I eat a ton of fresh fruits, vegetables, seeds, and nuts because of my mostly raw food diet. The third thing is dehydration, and this may very well be what caused my cramp this fine morning. I've been bad with my water the past week, because I've been busy with projects. With a glass of water in hand, I can positively say that I'll be back to my usual six glasses a day.
So if you ever feel one coming on, get up! Just stand, and walk around.
I'm confident that this will help you.