Because sometimes they go off all by themselves:
An arbitrator has ruled the US Airways pilot whose government-issue gun accidentally went off in flight can have his job back. Jim Langenhahn was fired after the 2008 incident and his union is welcoming the arbitration decision. ... Langenhahn's pistol shot a hole through the aircraft's fuselage, but the Department of Homeland Security helped his case when it faulted the design of the captain's holster. However, the Transportation Security Adminstration, which oversees the Federal Flight Deck Officer program claims, the same holster design has been used by thousands of pilots without incident.
Airline pilots were given the option of undergoing firearms training to carry guns in the cockpit in 2002. Langenhahn, a former Air Force pilot, claimed the gun discharged in the cockpit when he was putting it away before landing a flight out of Denver for Charlotte. No passengers or crew were hurt, and the aircraft landed without further incident.
Again, only two things have made airplanes safer against terrorism since 9/11: passenger vigilance and reinforced cockpit doors. Guns? Probably a bad idea.