最近使用Cordova中 的webview會(huì)出現(xiàn)以下報(bào)錯(cuò):
E/AndroidRuntime(13332): Java.lang.IllegalArgumentException: Receiver not registered: Android.widget.ZoomButtonsController$1@4162fc18
根據(jù)異常信息再參考一下WebView的源碼就可以知道ZoomButtonsController有一個(gè)register和unregister的過程将谊。但是這兩個(gè)過程是我們控制不了的疤剑,WebView有顯示控制的API但我們?cè)L問不過涉枫。
很多網(wǎng)上的回答都是在Activity的onDestroy里面加上這么一句:web.setVisibility(View.GONE);把WebView設(shè)置為GONE就可以了。
但是還是會(huì)報(bào)這樣的錯(cuò)誤筛严。仔細(xì)研究會(huì)發(fā)現(xiàn):webview的 ZoomButton的自動(dòng)隱藏是一個(gè)漸變的過程框沟,所以在逐漸消失的過程中如果調(diào)用了父容器的destroy方法晋修,就會(huì)導(dǎo)致Leaked六水。
所以要延遲1s再destroy:
if(null!=this.appView) {
if (null != this.appView.getView()) {
((WebView) this.appView.getView()).getSettings().setBuiltInZoomControls(true);
this.appView.getView().setVisibility(View.GONE);
long timeout
=ViewConfiguration.getZoomControlsTimeout();
new Timer().schedule(new TimerTask(){
@Override
public void run() {
try {
((WebView)appView.getView()).destroy();
}catch (Exception e){
}
}
}, timeout+1000L);
}
}
一般到這里就會(huì)結(jié)束了俺孙。但是我在CordovaActivity中應(yīng)用會(huì)發(fā)現(xiàn)還是沒用。那么我們看下是不是在父類中的ondestroy方法里提前把webview設(shè)置為GONE掷贾。
果不其然:
@Override
public void onDestroy() {
LOG.d(TAG, "CordovaActivity.onDestroy()");
super.onDestroy();
if (this.appView != null) {
appView.handleDestroy();
}
}
那么我們把這段:
if (this.appView != null) {
appView.handleDestroy();
}
注釋掉睛榄,移到外面延遲1s執(zhí)行即可