2013-05-26 61 views
23

我有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); 
} 

回答

35

當您嘗試它發生,使用從JavaScript界面​​調用的方法與UI進行交互。要以這種方式解決了這個問題:

class mJSInterface() 
{ 

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

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

    } 

} 
+0

哇,當你從JavaScript調用,很難察覺,感謝您指出我 – Pradeep

+1

有同樣的問題,但我絕對不與UI交互。如果調用錯誤的參數本地JavaScript功能發生 – matteo

+4

同樣的錯誤。 –

4

從@Leog

The same error occurs if you call the native javascript function with wrong parameters

突出的評論這是我的錯誤的根源

3

另一個原因可能是在WebViewCoreThread一個RuntimeException。任何異常接收@JavascriptInterface的通話將被記錄爲NPObject錯誤,如果在一個線程的WebView仍在運行後發生。對於問題的線索總體上不足。

與操控性上合適的線程JavaScript接口,通話兩不誤您的問題。

例A(NPObject錯誤):

@JavascriptInterface 
public void jsCall() { 
    Log.v(TAG, "Prepared NullPointerException on "+Thread.currentThread()); 
    String s = null; 
    s.length(); // This will cause NPObject error 
} 

例B.(空指針異常):

@JavascriptInterface 
public void jsCall() { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      Log.v(TAG, "Prepared NullPointerException on " + Thread.currentThread()); 
      String s = null; 
      s.length(); // This will throw NullPointerException 
     } 
    }).start(); 
} 

以此爲除@ Nico.S的答案。

0

操作的iframe在Android 4.4系統的WebView可能會導致類似的異常(未捕獲的ReferenceError:NPObject刪除),終於被我找到了解決辦法:

@Override 
public void onPageFinished(final WebView view, String finishUrl) { 
    super.onPageFinished(view, finishUrl); 
    // android 4.4 may lost value of 'Android' when operating iframe 
    view.addJavascriptInterface(Activity.this, "Android"); 
} 
相關問題