You probably got them when you weren't shaving, they were just hidden by the hair. If I remember correctly, when you get cold, your hair stands up, to trap warm air close to your body. With shaved legs, the folicles still 'stand up' but since there's no hair attached, you just see the bump.
I could be wrong, though.
As for prevention:
1. stop shaving
or
2. stay warm.