where I am wrong
You are wrong for having this code in your app.
how I can use it
You should not use it. Please allow the overflow menu to behave normally.
First, your code may not work on all devices and Android versions.
More importantly, what you are doing is telling your users that you think that you are more important than they are.
Each device, based on OS version and hardware, has its means of triggering the menu: either the ...
affordance in the action bar or the device's MENU button. Users want consistency between apps on their device. For users whose device has a MENU button, they will expect to use the MENU button in all apps to bring up menus where available. You are attempting to harm those users, by breaking their MENU button. Please do not do this. Please allow your app to behave the same way as the rest of the apps on those users' devices, where the MENU button brings up the overflow menu.
UPDATE
As of Android 4.4, the overflow ... affordance will appear in the action bar regardless of whether the device has a MENU button, and Google is putting a bit more pressure on device manufacturers to stop including a MENU button. I have blogged a bit more about this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…