Is there a way to prevent my knees from hurting and cracking?

Several things. This is a problem that necessitates a face-to-face meeting with your doctor. This will allow him/her to examine you, ask specific questions. And possibly order tests to find out what's wrong and what to do to help you.