NullPointerException in webview.java (android.webkit.WebView $ PrivateHandler.handleMessage)

Every few days I get a crash report for my application with the next stack trace or its small options (with different line numbers based on different versions of Android)

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 particular stack was on Android 2.3.4 on the HTC EVO 3D PG86100 device. There are several web views in my application for some atomic login scenarios.

How can I try to figure out how to fix this? I tried to find grepcode to find the source, but I cannot find a suitable line number, which makes sense. Is my greccode foo weak?

+6
source share
1 answer

I recently ran into this problem, and in my case, I was able to find out that fixing the secondary error caused this problem.

So, if you have not created a custom class that extends webview, this overrides onCheckIsTextEditor to always return true due to the suggestion in this Android error report ... I would stop reading here.

If you did this, then it seems that this fix has already been implemented by HTC and, for some reason, will cause it to fail to get focus. This problem manifested itself in two places for me, when placing sensory events in the presentation and establishing the visibility of the views. In my project, I installed WebViewClient and waited for the page to finish loading before setting the visibility of the web view to visible. This caused the following stack trace:

 java.lang.NullPointerException at android.webkit.WebView.navHandledKey(WebView.java:9353) at android.webkit.WebView.requestFocus(WebView.java:7910) at android.view.View.requestFocus(View.java:3718) at android.view.View.requestFocus(View.java:3696) at android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1803) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:474) at android.view.View.setFlags(View.java:4680) at android.view.View.setVisibility(View.java:3163) 

By wrapping the setVisibility call in try / catch, I was able to determine if I should override touch events or not. If I caught the event, it would mean that I have to disable the override. This is an example of what I did:

 try { webView.setVisibility(View.VISIBLE); } catch (NullPointerException e) { Log.i(TAG, "Error setting webview visibility due to overriding focus. It will be disabled."); webView.setOverrideOnCheckIsTextEditor(false); // Confirm window is visible webView.setVisibility(View.VISIBLE); } 

Now my custom webview looks like this, special attention is paid to the last two methods:

 /** * When using a webview in a dialog, there are issues relating to the keyboard not appearing when focusing on a text box. * This overrides a function to ensure the keyboard is shown when focusing on a text box in a webview. * See link for bug report and solution. * * @see http://code.google.com/p/android/issues/detail?id=7189 * @see http://stackoverflow.com/questions/12325720 * @author James O'Brien * @since 10/16/2012 */ public class FocusableWebView extends WebView { private boolean mOverrideCheckIsTextEditor = true; /** * Default Constructor * * @param context */ public FocusableWebView(Context context) { super(context); } /** * Default Constructor * * @param context * @param attrs */ public FocusableWebView(Context context, AttributeSet attrs) { super(context, attrs); } /** * Default Constructor * * @param context * @param attrs * @param defStyle */ public FocusableWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onCheckIsTextEditor() { if (mOverrideCheckIsTextEditor) { return true; } else { return super.onCheckIsTextEditor(); } } /** * Enable/Disable overriding of onCheckIsTextEditor to always return true. */ public void setOverrideOnCheckIsTextEditor(boolean foo) { mOverrideCheckIsTextEditor = foo; } } 

Sorry, I can’t explain in more detail why this fixes the problem. All I can assume is that his attitude towards focus and his dynamic shutdown works :)

** Update (12/11/12) **

It was possible to solve both problems presented above. This seemed to be a web browsing focus issue. Now my code looks like

 webView.setVisibility(View.VISIBLE); webView.setFocusable(true); webView.requestFocus(); 

I would advise making sure that you explicitly set the focusable attribute to "true" or call setFocusable (true).

+5
source

Source: https://habr.com/ru/post/924883/


All Articles