Hello Zhao.
You're right in thinking that Lehrer is the object, but it's the object of "danken," whose object is always dative. there are maybe 25 or so verbs that always take a dative object. Some others are "helfen", "antworten," and "glauben."
Erklärung is accusative here, because it follows the preposition "für," which always takes accusative. Such prepositions are bis, durch, für, ohne, gegen, and um