how do I avoid this trap

go back and look at it (5 ...Qh4 is a nasty counter; white should have captured 5. Bxf7+)
——
but i am guessing that you are sick of the fried liver in general
if this is the case then a good way to avoid the fried liver is to play is to play 3 ...Bc5 before ...Nf6; this allows you to develop and keep an eye on g5 (so white knight cant jump in yet-) and you can get g8 knight out next and castle
there are lines to know where black takes the fried liver head on but it gets really crazy
nevertheless, look at all these games afterward and see how different moves could have been played
the game only lasted 5 moves so it should be easy to work with this game in an analysis

You're resigning too early. Keep on playing your opponent will make a mistake.
You can try the Giuoco Piano (the line daxypoo) mentioned where you play Bc5 before playing Nf6... see how the queen is eyeing that g5 square so if the knight lands there you capture. If not you can can play Nc6 and then castle, if your opponent trades both bishop and knight for your rook and pawn that trade is favorable to you
Or you can try
Where black is down a pawn but has compensation.... Most common moves here for white are Qf3, Bd3 and Be2 so be ready for those
Or as daxypoo mentioned you can try and accept it but it's not recommended but it's fun to play for both sides

For this one, just use an engine to see the best continuation. I use Lichess because it's not only free, but it offers an opening explorer which is very useful when you want to see what most people are playing. It can also be used to discover traps and other cool things but you don't have to worry about it.

Before you start exploring moves with engine, look up some Fried Liver videos on YouTube. There are many (quite entertaining) videos available that guide you through the main lines. i can recommend checking them out.
As mentioned before, you resigned too early. Not just to see if you could fight back, because you were better. The engine analysis evaluates the position to be roughly equal. But, technically, your position was better. Your chess will improve if you stop throwing games for no reason.
Depends how much you like the opening traps. 1. e4 e5 is quite notorious for its opening traps. You could look them up and try and remember them. You could also switch to a different opening move if you want to cut out all these opening traps. You don't need to study anything new, just start playing 1. ...c5 and the traps will end.