我在Android中的Webview有问题,它是JavascriptInterfaces.
我将一个字符串传递给JavascriptInterface.调试时,我在Android应用程序中收到正确的字符串.问题:有时我得到一个Uncaught Error:在NPObject上调用错误的方法.
有人知道为什么吗?
Java中的接口:
public class JSInterfaceGame extends JSInterface {
@JavascriptInterface
public void setShareText(String share){
shareText = share;
if(mJSInterfaceListener != null)
mJSInterfaceListener.onParametersChanged(SHARE_TEXT);
}
Run Code Online (Sandbox Code Playgroud)
片段中onCreateView-Method的初始化:
online = (WebView) rootView.findViewById(R.id.online);
online.setWebViewClient(new WISWebviewClient() {
@Override
public void onStatusChanged(final WebView view, int progress, long duration) {
//unrelated
}
});
WebSettings ws = online.getSettings();
ws.setJavaScriptEnabled(true);
ws.setUserAgentString(USER_AGENT);
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
ws.setRenderPriority(WebSettings.RenderPriority.HIGH);
SharedPreferences settings = getActivity().getSharedPreferences(GameActivity.PREFERENCES, Context.MODE_PRIVATE);
mJSInterface = new JSInterfaceGame();
mJSInterface.setJSInterfaceListener(this); // Defined elsewhere in this class.
mJSInterface.setPlayerName(settings.getString(GameActivity.PREFS_PlAYERNAME, null));
online.addJavascriptInterface(mJSInterface, "JSInterface");
online.loadUrl("http://myurl.something");
Run Code Online (Sandbox Code Playgroud)
在Javascript中调用:
function makeShareText() {
var …Run Code Online (Sandbox Code Playgroud)