最近使用Cordova中 的webview會出現以下報錯:
E/AndroidRuntime(13332): Java.lang.IllegalArgumentException: Receiver not registered: Android.widget.ZoomButtonsController$1@4162fc18
根據異常信息再參考一下WebView的源碼就可以知道ZoomButtonsController有一個register和unregister的過程。但是這兩個過程是我們控制不了的,WebView有顯示控制的API但我們訪問不過。
很多網上的回答都是在Activity的onDestroy里面加上這么一句:web.setVisibility(View.GONE);把WebView設置為GONE就可以了。
但是還是會報這樣的錯誤。仔細研究會發現:webview的 ZoomButton的自動隱藏是一個漸變的過程,所以在逐漸消失的過程中如果調用了父容器的destroy方法,就會導致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);
}
}
一般到這里就會結束了。但是我在CordovaActivity中應用會發現還是沒用。那么我們看下是不是在父類中的ondestroy方法里提前把webview設置為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執行即可