有时候抛出未被捕获的错误:错误调用方法上NPObject在Android [英] Sometimes throws Uncaught Error: Error calling method on NPObject on Android

查看:204
本文介绍了有时候抛出未被捕获的错误:错误调用方法上NPObject在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于机器人web视图和JavaScript接口问题。 我打电话传递一个字符串JavascriptInterface的方法。我调试它,我接受正确的字符串在Java中。问题是,有时我得到一个未捕获的错误:错误调用方法上NPObject。有谁知道为什么吗?


类:

 公共类JSInterfaceGame扩展JSInterface {

@JavascriptInterface
公共无效setShareText(字符串股){
    shareText =份额;
    如果(mJSInterfaceListener!= NULL)
        mJSInterfaceListener.onParametersChanged(SHARE_TEXT);
}
 

GameFragment(onCreateView法):

 在线=(web视图)rootView.findViewById(R.id.online);
    WebSettings WS = online.getSettings();
    ws.setJavaScriptEnabled(真正的);
    ws.setUserAgentString(USER_AGENT);
    ws.setCacheMode(WebSettings.LOAD_DEFAULT);
    ws.setRenderPriority(WebSettings.RenderPriority.HIGH);

    online.setWebViewClient(新WISWebviewClient(){

        @覆盖
        公共无效onStatusChanged(最终的WebView视图,INT进步,持续时间长){
           //无关
        }
    });

    mJSInterface =新JSInterfaceGame();
    mJSInterface.setJSInterfaceListener(本); //我自己的监听器
    。共享preferences设置= getActivity()getShared preferences(GameActivity preFERENCES,Context.MODE_PRIVATE);
    mJSInterface.setPlayerName(settings.getString(GameActivity preFS_PlAYERNAME,NULL));
    online.addJavascriptInterface(mJSInterface,JSInterface);
    online.loadUrl(HTTP:// ...);
 

Javascript的部分:

  //文本替换
 功能replaceText(文字){
    如果(的arguments.length&其中; = 1)
        返回文本;
    VAR tokenCount的arguments.length =  -  2;
    对于(VAR令牌= 0;令牌LT; = tokenCount;令牌++)
    文= text.replace(新的RegExp(\\ {+令牌+\\},GI),参数[令牌+ 1]);
    返回文本;
  }

  功能loadMain(){
    VAR T = replaceText(语言[sharelink_description],http://website.org');
    的console.log(typeof运算T); //总是串
    JSInterface.setShareText(T);
  }

 $(文件)。就绪(函数(){
      loadMain();
  });
 

解决方案

在尝试,使用方法,从JavaScript接口调用,与UI交互时发生。要解决它以这种方式:

 类mJSInterface()
{

公共无效myFunction的()
{
    runOnUiThread(新的Runnable(){

            公共无效的run(){
                // code与UI交互
            }
        });

    }

}
 

I am having problems regarding Androids Webview and Javascript Interfaces. I am calling a method passing a String to the JavascriptInterface. I debugged it and I receive the right String in Java. The problem is that sometimes I get an uncaught Error: Error calling method on NPObject. Does anybody know why?


Classes:

public class JSInterfaceGame extends JSInterface {

@JavascriptInterface
public void setShareText(String share){
    shareText = share;
    if(mJSInterfaceListener != null)
        mJSInterfaceListener.onParametersChanged(SHARE_TEXT);
}

GameFragment (onCreateView-Method):

    online = (WebView) rootView.findViewById(R.id.online);
    WebSettings ws = online.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setUserAgentString(USER_AGENT);
    ws.setCacheMode(WebSettings.LOAD_DEFAULT);
    ws.setRenderPriority(WebSettings.RenderPriority.HIGH);

    online.setWebViewClient(new WISWebviewClient() {

        @Override
        public void onStatusChanged(final WebView view, int progress, long duration) {
           //unrelated
        }
    });

    mJSInterface = new JSInterfaceGame();
    mJSInterface.setJSInterfaceListener(this); //my own listener
    SharedPreferences settings = getActivity().getSharedPreferences(GameActivity.PREFERENCES, Context.MODE_PRIVATE);
    mJSInterface.setPlayerName(settings.getString(GameActivity.PREFS_PlAYERNAME, null));
    online.addJavascriptInterface(mJSInterface, JSInterface");
    online.loadUrl("http://....");

Javascript Part:

 //Text Replacements
 function replaceText( text ) {
    if ( arguments.length <= 1 )
        return text;
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    return text;
  }

  function loadMain() {
    var t = replaceText(language["sharelink_description"], 'http://website.org');
    console.log(typeof t); //always string
    JSInterface.setShareText(t);
  }

 $(document).ready(function(){
      loadMain();
  });

解决方案

It happens when you try, using method called from javascript interface, to interact with UI. To solved it in this way:

class mJSInterface()
{

public void myFunction()
{
    runOnUiThread(new Runnable() {

            public void run() {
                //Code that interact with UI
            }
        });

    }

}

这篇关于有时候抛出未被捕获的错误:错误调用方法上NPObject在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆