1
這個問題在這裏並不新鮮,但我仍然無法得到它出錯的地方。我只是想評估一些JavaScript。Android在調用API的WebView中調用JavaScript函數<= 18
我簡單的代碼看起來像:
WebView wv = new WebView(context);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
JavaBridgeToJS bridgeToJS = new JavaBridgeToJS();
wv.addJavascriptInterface(bridgeToJS, "javaCallback");
String src = "javascript: var result= 3 + 3; window.javaCallback.resultOfAdd(result)";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
Log.d(TAG, "Newer than JellyBean.");
ValueCallback<String> result = new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.d(TAG, "Got in onReceiveValue: " + value);
}
};
wv.evaluateJavascript(src, result);
}else {
Log.d(TAG, "Older than Kitkat");
wv.loadUrl(src);
}
和
public class JavaBridgeToJS {
@JavascriptInterface
public void resultOfAdd(String result) {
Log.d(TAG, "Here is: " + result);
}
}
這是工作好API 19+,即使沒有如果用的版本檢查(我的意思是與loadUrl以及評估JavaScript一起工作)
但是當我嘗試API 18,不調用java中的回調。 我已到目前爲止,我有沒有運氣環顧四周(remove line break,或quotes missing(可能受感染,看着手機上),等等...)
。任何想法?
編輯
好吧,我加入的代碼在辦案JS裏面的錯誤,我已經錯過了什麼。
wv.setWebChromeClient(new CustomWebChromeClient());
爲
public class CustomWebChromeClient extends WebChromeClient {
private CustomWebChromeClient() {
super();
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
UILog.d(TAG, consoleMessage.message());
return super.onConsoleMessage(consoleMessage);
}
}
,我和他驚訝:
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1896 (Landroid/webkit/PermissionRequest;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.WebChromeClient$FileChooserParams', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1899 (Landroid/webkit/WebChromeClient$FileChooserParams;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
D/WebViewDoll: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined
E/Web Console: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined at null:1
**更新**我做了新的項目,以防萬一,但仍然沒有。 我想這應該工作[github回購](https://github.com/ThinkDeeper/webview_api18) – ThinkDeep