Java 来自 Webview 的空指针异常

Java nullpointer exception from Webview

我试图搜索这个问题,但这个错误看起来与 this error 不同(我的 google play 版本较新,请注意错误在不同的行WebViewClassic 的,因此可能是不同的错误或在较新版本中相同)但我从 webview

收到了完全相同的错误异常
java.lang.NullPointerException
at android.webkit.WebViewClassic$WebViewInputConnection.setNewText(WebViewClassic.java:583)
at android.webkit.WebViewClassic$WebViewInputConnection.setComposingText(WebViewClassic.java:323)
at android.webkit.WebViewClassic$WebViewInputConnection.commitText(WebViewClassic.java:339)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

但是我没有直接使用任何 webview class。但是,这个问题可能与 AdView 有关,但我不明白如何。我的代码如下:

 @Override
 public void onPause() {
    //TODO: these lines are created because onPuase of Adview doesn't work

     if ( adView != null ) {
         adView.pause();
         adView.destroy();
         adView = null;

         Log.i(ApplicationData.APP_TAG, TAG + ": OnPause, pausing the Adview");
     }

     super.onPause();

 }

错误出现在 Log.i 行之后,我本以为会在调用 AdView 时出现此错误,但之后不会出现。此错误可能来自线程操作吗?知道如何避免这个问题吗?

https://developer.android.com/reference/com/google/android/gms/ads/AdView.html and the document for AdView.destroy()中的例子,你应该只在onPause()中调用adView.pause(),然后在onDestroy()中调用adView.destroy()

堆栈转储看起来像是另一个线程正在访问已销毁的 AdView 实例。