Cavities in the lungs can be caused by bacterial infection, fungal infection, inflammatory diseases, and cancer.