Android: a problem with a custom webview and options menu
I've spent weeks designing a custom webview, for an app I am building. I have a class (let's call it myWebView) that extends the android WebView. I declared myWebView in my main.xml, just as you would any other UI elements accept i prepended myWebView with the package name. Everything worked fine and dandy.
After weeks of getting all the functionality on my extended class working it came time to implement an options menu. Compared with what I had been doing I thought this would be a relatively simple task. I overwrote onCreateOptionsMenu(), and tried to inflate my menu from an xml file. When I ran my app in the emulator and pressed the menu button, nothing happened. My code for onCreateOptionsMenu() wasn't flawed. I tried it in a dummy activity that had a simple main.xml that only contained a regular webview. The menu worked in the dummy activity. I then tried the various other ways to populate a menu (using the different implementations of menu.add()), and they all worked my dummy activity but they didn't work in my actual app. I deleted everything from my activity except for what was in onCreate and onCreateOptionsMenu, to see if anything from one my other methods was interfering with the menu creation. That also accomplished nothing. I put a log statement into onCreateOptionsMenu() to see if the method was even being called. Lo-and-behold it wasn't being called when i pushed the menu button. Finally I went into my main.xml and deleted the tag for myWebView from t开发者_开发知识库he layout. After doing that the app ran and menu worked.
So for some reason, using my custom web view prevents menu's from working. Has anyone else encountered this problem? I find it hard to believe that you can't use options menus with custom ui elements, so there must be some work around. Conversely, has anyone built a custom webview and implemented an options menu successfully, which would mean that i have to go and re-examine my code. I'll put my code up if anyone thinks it will help, but I am 99.99% positive that everything is coded correctly and according to best practices, and the code for my extended class is massive and would require a great deal of scrubbing. Based on what I have said here though, does anyone have any ideas?
Strange. I use webview and an options menu, works fine.
I don't extend WebView... I extend activity, then config the webview in the XML or add the webview to "content view".
Do all the websettings and add my own chromeclient and viewclient to change the functionality of webview.
I just switched from adding menus programmatically to using an XML descriptor. So I know it still works.
I'm using SDK 7 (2.1.x)
If you need to extend webview, post your reasons and some code and hopefully I can help.
I just figured it out actually. I override keypad events to do nothing and that's what was preventing it from occurring. apparently it's a pretty common error that people make. I must have just not hit that magic combination of search terms for google before. thank you for ur offer of help though. I hope my mistake will help you in android journeys
精彩评论