Criminal trespassing is the legal term for trespassing onto someone else's land or using that person's chattel without permission. On the whole, law enforcement agents such as sheriffs, park rangers and police officers enforce the laws for trespassing. In cases where charges are brought against you for crossing onto another person's land, you do have the right to defend yourself.
There are several defenses to trespassing. One is that you have consent to be there. Here's an example of how this may come up. Perhaps the homeowner is not at home, but you have gained permission to enter the property. If a neighbor sees you and believes you're trespassing, he or she may call the police or local sheriff. While you may be arrested initially if the homeowner is not able to be contacted, it's likely the charges would be dropped as soon as the homeowner cleared up the confusion.
Public necessity is another good defense for trespassing. This is why you have to trespass to protect the public due to an emergency. For instance, if there is a violent storm headed toward you and it's faster to cross a person's yard to get to shelter than it is to walk around the land, you may be in the right. The important thing to remember in that case is that the trespassing must be reasonable.
Your charge for trespassing may be due to a misunderstanding, immediate need to cross the land or for other reasons. Your attorney can help you protect yourself and explain the situation, so you can move forward with your life.
Source: FindLaw, "Are There Defenses to Criminal Trespassing?," Aditi Mukherji, JD, accessed June 20, 2017