項目經理說,Android沒有處理彈框,點擊按鈕沒有反應,iOS就能夠。因而就複寫了onJsAlert、onJsConfirm、onJsPrompt三個方法,來處理頁面上的彈框。寫完後滿心喜悅的覺得能夠了,沒想到項目經理說,點擊按鈕異常退出;而後本身查看日誌:java
08-06 10:41:53.023 29277-29339/com.medicine E/chromium: [ERROR:child_thread_impl.cc(762)] Request for unknown Channel-associated interface: ui::mojom::GpuMain 08-06 10:41:53.036 29277-29338/com.medicine E/libEGL: cache file failed CRC check
然並卵,這樣的日誌並不能查找出問題,還好紅米手機有捕捉異常的功能,詳細異常以下:android
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** Build fingerprint: 'Xiaomi/hennessy/hennessy:5.0.2/LRX22G/V9.6.1.0.LHNCNFD:user/release-keys' Revision: '0' ABI: 'arm64' pid: 27850, tid: 27850, name: medicine >>> com.medicine <<< signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr -------- Abort message: '[FATAL:jni_android.cc(243)] Please include Java exception stack in crash report ' x0 0000000000000000 x1 0000000000006cca x2 0000000000000006 x3 0000007fae06a0f0 x4 0000007fae06a0f0 x5 0000000000000005 x6 0000000000000001 x7 0000000000000020 x8 0000000000000083 x9 0000007fd82d9043 x10 0000007fd82d8ff8 x11 0000000000000001 x12 0000000000000001 x13 0000000000000054 x14 0000007f9d9dd7c8 x15 0000007fae035bc0 x16 0000007fae02d7e0 x17 0000007fadff258c x18 0000000000000000 x19 0000007fae06a0f0 x20 0000007fae06a3a8 x21 0000007fae033000 x22 000000000000000b x23 0000000000000006 x24 0000007fd82d98f0 x25 0000007fd82d9ab8 x26 0000000061100021 x27 000000007a90001d x28 00000055bc8f0a60 x29 0000007fd82d9320 x30 0000007fadfaeb6c sp 0000007fd82d9320 pc 0000007fadff2594 pstate 0000000060000000 backtrace: #00 pc 0000000000061594 /system/lib64/libc.so (tgkill+8) #01 pc 000000000001db68 /system/lib64/libc.so (pthread_kill+160) #02 pc 000000000001f09c /system/lib64/libc.so (raise+28) #03 pc 0000000000018abc /system/lib64/libc.so (abort+60) #04 pc 0000000000771f20 /system/app/WebViewGoogle/lib/arm64/libwebviewchromium.so
雖然獲取到了更多的信息,我仍是想哭,仍是沒有一點用處,都是底層框架的錯誤信息,對於查找錯誤仍是雲裏霧裏,只好去排查代碼。既然是複寫onJsAlert、onJsConfirm、onJsPrompt三個方法後,報錯就查看具體複寫的代碼,一下是複寫代碼詳情:web
@Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { new AlertDialog.Builder(view.getContext()) .setTitle("對話框") .setMessage(message) .setPositiveButton("肯定", (dialog, which) -> { result.confirm(); dialog.dismiss(); }) .setCancelable(false) .show(); return true; } @Override public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { new AlertDialog.Builder(view.getContext()) .setTitle("對話框") .setMessage(message) .setPositiveButton("肯定", ((dialog, i) -> { result.confirm(); dialog.dismiss(); })) .setNegativeButton("取消", (dialogInterface, i) -> result.cancel()) .setOnCancelListener(dialogInterface -> result.cancel()) .show(); return true; } @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { final EditText et = new EditText(view.getContext()); et.setSingleLine(); et.setText(defaultValue); new AlertDialog.Builder(view.getContext()) .setTitle("對話框") .setMessage(message) .setView(et) .setPositiveButton("肯定", ((dialog, i) -> { result.confirm(et.getText().toString()); dialog.dismiss(); })) .setNegativeButton("取消", (dialogInterface, i) -> result.cancel()) .setOnCancelListener(dialogInterface -> result.cancel()) .show(); return true; }
查看代碼的時候,發現new AlerDialog
的時候用的是view.getContext()
,而後發現new WebView()
的時候用的是getApplicationContext()
;咱們知道AlertDialog的Context只能是Activity,因此發現問題;把getApplicationContext()改成當前Activity後,問題就解決了。app
測試人員說,Android端在選擇時間的時候失敗,只能夠選擇當前時間。因而本身測試,第一次進入能夠選擇時間,沒有問題;可是當鎖屏後,再次進入頁面後,能夠彈出日期選擇框,滾動日期的時候,明顯發現日期滾動不暢,而且預期的時間並無出現,而是默認的當前時間。框架
找到問題所在,而後查找問題根源在哪裏:既然是在鎖屏後出現問題,會不會是onPause()和onResume()方法裏面的問題呢;心中有了這樣的想法後就把onPause()和onResume()註釋了,再次運行,發現日期選擇正常了;如此看來,的確是onPause()和onResume()方法裏面出現問題了。這兩個方法是從網上找來的,具體代碼以下:ide
@Override protected void onResume() { super.onResume(); if (mWebView != null) { mWebView.onResume(); } } @Override protected void onPause() { super.onPause(); if (mWebView != null) { mWebView.onPause(); mWebView.pauseTimers(); } }
copy的代碼也沒有深究,可是發現有個方法沒有成對出現mWebView.pauseTimers()
,而後查看源代碼,文檔解釋爲:佈局
Pauses all layout, parsing, and JavaScript timers for all WebViews. This is a global requests, not restricted to just this WebView. This can be useful if the application has been paused.測試
機器翻譯爲:暫停全部WebView的全部佈局,解析和JavaScript計時器。此是全局請求,不只限於此WebView。若是應用程序已暫停,這可能頗有用。ui
而後再WebView源碼中發現pauseTimers()
方法下面就是resumeTimers()
方法,喜出望外,文檔解釋爲:this
Resumes all layout, parsing, and JavaScript timers for all WebViews. This will resume dispatching all timers.
機器翻譯爲:恢復全部WebView的全部佈局,解析和JavaScript計時器。 這將繼續調度全部計時器。
因而,恢復註釋的代碼,在Activity的onResume()方法中加上mWebView.resumeTimers()
,運行項目,發現問題解決。所以,具體問題是在Activity息屏後調用webview的pauseTimers()方法,把js功能暫停;可是在onResume()沒有恢復運行,因此形成這個問題。