Same problem happened with me when I try to show a popup menu in an activity.
I also got the same exception but I encountered problems and resolved it by providing the right context.
// at this line
Dialog dialog = new Dialog(getApplicationContext());
Use
YourActivityName.this
instead of getApplicationContext()
and yes it worked for me may it will help someone else.