Our body is designed to battle almost any disease possible and to regenerate if hurt. However, there are those things that our body simply cannot fight on its own and it actually needs our help. That is why our body created a system of warnings to tell us that there is something wrong with it. Our unhealthy lifestyle and what we eat can do some serious harm to our bodies and sometimes you only need to tweak those two things in order to help your body. But sometimes you need to do a lot more.
Leg cramps could happen at night in the middle of the sleep or when you make some random move during the day. Either way, the main cause for it is an inadequate blood supply because of narrow arteries. Another cause could be plain exhaustion.
To prevent that, you need to eat foods that have lots of potassium (oranges, bananas, tomatoes, etc.). Bananas are maybe the best possible solution. Also, you need to drink plenty of water.