我有web視圖Android中的問題和它的JavascriptInterfaces。有時會拋出未捕獲的錯誤:錯誤呼籲NPObject方法在Android
我傳遞一個字符串JavascriptInterface。在調試它時,我會在我的Android應用程序中收到正確的字符串。問題:有時候我得到一個未捕獲的錯誤:在NPObject上調用方法的錯誤。
有人知道爲什麼嗎?
Java中的接口:
public class JSInterfaceGame extends JSInterface {
@JavascriptInterface
public void setShareText(String share){
shareText = share;
if(mJSInterfaceListener != null)
mJSInterfaceListener.onParametersChanged(SHARE_TEXT);
}
的片段內的onCreateView法初始化:
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");
呼叫在Javascript:
function makeShareText() {
var text = "Some text";
console.log(typeof text); // Always a string.
JSInterface.setShareText(text);
}
哇,當你從JavaScript調用,很難察覺,感謝您指出我 – Pradeep
有同樣的問題,但我絕對不與UI交互。如果調用錯誤的參數本地JavaScript功能發生 – matteo
同樣的錯誤。 –