Lungs hurt after running or bike riding. How to prevent this?

Get checked out. You may have exercise induced asthma, and all other treatment/ suggestions would be futile. Always start new exercise slowly and listen to your body.