Every few days I get a crash report for my application with the following stack trace, or small variants thereof (with different line numbers based on different android versions)
java.lang.NullPointerException at
WebView.java:8241:in `android.webkit.WebView$PrivateHandler.handleMessage'
Handler.java:99:in `android.os.Handler.dispatchMessage'
Looper.java:150:in `android.os.Looper.loop'
ActivityThread.java:4293:in `android.app.ActivityThread.main'
Method.java:-2:in `java.lang.reflect.Method.invokeNative'
Method.java:507:in `java.lang.reflect.Method.invoke'
ZygoteInit.java:849:in `com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run'
ZygoteInit.java:607:in `com.android.internal.os.ZygoteInit.main'
NativeStart.java:-2:in `dalvik.system.NativeStart.main'
This specific stack was on Android 2.3.4 on a HTC EVO 3D PG86100 device.
My app does host several webviews for some oAuth-related login scenarios.
How should I go about trying to figure out how to fix this? I’ve tried looking on grepcode to find the source, but I’m unable to find a matching line number that makes sense. Is my Grepcode-fu weak?
I ran into this issue recently and in my case I managed to figure out that a secondary bug fix was causing this issue.
So, if you didn’t create a custom class which extends webview, that overrides onCheckIsTextEditor to always return true because of a suggestion in this android bug report… I would stop reading here.
If you have done that, then it appears that this fix has already been implemented by HTC and for some reason causes it to crash when it receives focus. This issue manifested itself in two places for me, when placing touch events on the view and by setting the views visibility. In my project I set a WebViewClient and waited until the page had finished loading before setting the webview’s visibility to visible. This caused the following stack trace:
By wrapping the setVisibility call in a try/catch I was able to determine if I should override the touch events or not. If I caught the event, that would mean I should disable the override. This is an example of what I did:
My custom webview now looks like this, special attention paid to the final two methods:
Sorry I can’t go into more detail about why this fixes the issue. All I can assume is that its related to focus and dynamically disabling it works a treat 🙂
** Update (12/11/12) **
Managed to solve both issues presented above. It seemed to be an issue with the focus of the webview. My code now looks like this
I would suggest making sure that you explicitly set the focusable attribute to ‘true’ or calling setFocusable(true).