android webview 调用webview类中的销毁方法并不能完全销毁webview对象,只能销毁webviewCore中的一些线程,源码中的销毁方法如下:
/**
* Destroy the internal state of the WebView. This method should be called
* after the WebView has been removed from the view system. No other
* methods may be called on a WebView after destroy.
*/
public void destroy() {
clearTextEntry(false);
if (mWebViewCore != null) {
// Set the handlers to null before destroying WebViewCore so no
// more messages will be posted.
mCallbackProxy.setWebViewClient(null);
mCallbackProxy.setWebChromeClient(null);
// Tell WebViewCore to destroy itself
synchronized (this) {
WebViewCore webViewCore = mWebViewCore;
mWebViewCore = null; // prevent using partial webViewCore
webViewCore.destroy();
}
// Remove any pending messages that might not be serviced yet.
mPrivateHandler.removeCallbacksAndMessages(null);
mCallbackProxy.removeCallbacksAndMessages(null);
// Wake up the WebCore thread just in case it is waiting for a
// javascript dialog.
synchronized (mCallbackProxy) {
mCallbackProxy.notify();
}
}
if (mNativeClass != 0) {
nativeDestroy();
mNativeClass = 0;
}
}
由于这样进行销毁导致android2.2以下的直接调用了Destory时和代码写的稍有不慎(如webview对象到处引用后,只把当前的引用赋值为为null,并没有把其它地方的引用赋值为null),导致再次取webSettings时出现空指针异常
但在4.0.3时google很快修复了此问题,4.0的这个方法的代码如下:
/**
* Return the WebSettings object used to control the settings for this
* WebView.
* @return A WebSettings object that can be used to control this WebView's
* settings.
*/
public WebSettings getSettings() {
checkThread();
return (mWebViewCore != null) ? mWebViewCore.getSettings() : null;
}
2.2的源码方法如下:
/**
* Return the WebSettings object used to control the settings for this
* WebView.
* @return A WebSettings object that can be used to control this WebView's
* settings.
*/
public WebSettings getSettings() {
return mWebViewCore.getSettings();
}
/**
* Destroy the internal state of the WebView. This method should be called
* after the WebView has been removed from the view system. No other
* methods may be called on a WebView after destroy.
*/
public void destroy() {
clearTextEntry(false);
if (mWebViewCore != null) {
// Set the handlers to null before destroying WebViewCore so no
// more messages will be posted.
mCallbackProxy.setWebViewClient(null);
mCallbackProxy.setWebChromeClient(null);
// Tell WebViewCore to destroy itself
synchronized (this) {
WebViewCore webViewCore = mWebViewCore;
mWebViewCore = null; // prevent using partial webViewCore
webViewCore.destroy();
}
// Remove any pending messages that might not be serviced yet.
mPrivateHandler.removeCallbacksAndMessages(null);
mCallbackProxy.removeCallbacksAndMessages(null);
// Wake up the WebCore thread just in case it is waiting for a
// javascript dialog.
synchronized (mCallbackProxy) {
mCallbackProxy.notify();
}
}
if (mNativeClass != 0) {
nativeDestroy();
mNativeClass = 0;
}
}
由于这样进行销毁导致android2.2以下的直接调用了Destory时和代码写的稍有不慎(如webview对象到处引用后,只把当前的引用赋值为为null,并没有把其它地方的引用赋值为null),导致再次取webSettings时出现空指针异常
但在4.0.3时google很快修复了此问题,4.0的这个方法的代码如下:
/**
* Return the WebSettings object used to control the settings for this
* WebView.
* @return A WebSettings object that can be used to control this WebView's
* settings.
*/
public WebSettings getSettings() {
checkThread();
return (mWebViewCore != null) ? mWebViewCore.getSettings() : null;
}
2.2的源码方法如下:
/**
* Return the WebSettings object used to control the settings for this
* WebView.
* @return A WebSettings object that can be used to control this WebView's
* settings.
*/
public WebSettings getSettings() {
return mWebViewCore.getSettings();
}