gpt4 book ai didi

android - webview.java 中的 NullPointerException (android.webkit.WebView$PrivateHandler.handleMessage)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:18 30 4
gpt4 key购买 nike

每隔几天,我都会收到一份应用程序崩溃报告,其中包含以下堆栈跟踪或其小变体(根据不同的 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'

这个特定堆栈是在 HTC EVO 3D PG86100 设备上的 Android 2.3.4 上。我的应用确实为一些与 oAuth 相关的登录场景托管了多个 Web View 。

我应该如何着手解决这个问题?我试过在 grepcode 上查找源代码,但找不到有意义的匹配行号。我的 Grepcode-fu 很弱吗?

最佳答案

我最近遇到了这个问题,就我而言,我设法弄清楚是次要错误修复导致了这个问题。

因此,如果您没有创建扩展 webview 的自定义类,那么由于此 android bug report 中的建议,它会覆盖 onCheckIsTextEditor 以始终返回 true ...我会在这里停止阅读。

如果您这样做了,那么 HTC 似乎已经实现了此修复程序,并且由于某种原因导致它在获得焦点时崩溃。对我来说,这个问题在两个地方表现出来,在 View 上放置触摸事件和设置 View 可见性。在我的项目中,我设置了一个 WebViewClient 并等待页面加载完成,然后再将 webview 的可见性设置为可见。这导致了以下堆栈跟踪:

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)

通过将 setVisibility 调用包装在 try/catch 中,我能够确定是否应该覆盖触摸事件。如果我捕获到该事件,则意味着我应该禁用覆盖。这是我所做的一个例子:

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);
}

我的自定义 webview 现在看起来像这样,特别注意最后两个方法:

/**
* 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;
}
}

抱歉,我无法更详细地说明为什么这可以解决问题。我所能假设的是,它与焦点有关并动态禁用它是一种享受:)

** 更新(2012 年 12 月 11 日)**

设法解决了上述两个问题。这似乎是 webview 焦点的问题。我的代码现在看起来像这样

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

我建议确保您将 focusable 属性显式设置为“true”或调用 setFocusable(true)。

关于android - webview.java 中的 NullPointerException (android.webkit.WebView$PrivateHandler.handleMessage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12325720/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com