2013-12-14 65 views
3

我正在使用以下MathJax應用程序代碼。 http://cs.jsu.edu/wordpress/?p=498#comment-217在Android 4.4上加載資源文件時未捕獲到ReferenceError

在下面的函數中,我試圖從資產目錄加載文件。

public static boolean makeMathView_new(WebView webview) { 
      webview.getSettings().setRenderPriority(RenderPriority.HIGH); 
      webview.getSettings().setJavaScriptEnabled(true); 
      webview.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>" 
        +"MathJax.Hub.Config({ messageStyle: 'none', showMathMenu: false, jax: ['input/TeX','output/HTML-CSS'], " 
        +"extensions: ['tex2jax.js'],"   
       + "'HTML-CSS': { scale: 100 },"    
        +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'] } });</script>" 
       +"<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script>" 
       + "<span id='math'></span>","text/html","utf-8", ""); 


     return true; 
    } 

在Android 4.4模擬器上運行時,出現以下錯誤。

V/WebViewChromium(1342): Binding Chromium to the background looper Looper{b1da1340} 
I/chromium(1342): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0 
I/BrowserProcessMain(1342): Initializing chromium process, renderers=0 
W/chromium(1342): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation 
E/chromium(1342): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
E/chromium(1342): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
E/chromium(1342): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
E/chromium(1342): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
E/chromium(1342): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed 

I/chromium(1101): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1) 

更新:結合ksasq的建議後,這是我的新代碼,但它仍然無法正常工作。

webview.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>" 
     + "function setupMathJax() {" 
       + "MathJax.Hub.Config({ messageStyle: 'none', showMathMenu: false, jax: ['input/TeX','output/HTML-CSS'], " 
        +"extensions: ['tex2jax.js'],'HTML-CSS': { scale: 100 },"    
        +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'] } });" 
    + "}" 
       + "</script>" 
       + "<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script>" 
    + "<body onload='setupMathJax()'>" 
       + "<span id='math'></span>" 
    + "</body>" 
       ,"text/html","utf-8", ""); 

回答

0

它看起來像MathJax變量將不會被設置,直到您裝入<script>標籤。你應該把它移入一個onload listener。喜歡的東西:

<script type='text/x-mathjax-config'> 
    function setupMathJax() { 
    MathJax.Hub.Config(...); 
    } 
</script> 
<script type='text/javascript' src='file:///android_asset/MathJax/MathJax.js'></script> 
<body onload='setupMathJax()'> 
<span id='math'></span> 
</body> 
+0

謝謝,但它仍然沒有工作。按照您的建議,我已經完成了代碼更改。請檢查我上面的新代碼。 –

+0

您是否嘗試過使用遠程調試來確保MathJax腳本正在加載?它可能有助於指出問題出在哪裏。 (請參閱https://developers.google.com/chrome-developer-tools/docs/remote-debugging#debugging-webviews) – ksasq

0

也許包含第一<script>function setupMathJax()之前,你的第二個<script>加載了您MathJax文件,導致異常的地方MathJax is not defnied是越來越執行。嘗試顛倒這些標籤的順序。

4

您必須將所有行loadUrl(...)替換爲evaluateJavascript(...)。但這僅僅是適用於KitKat(API 19或以上),那麼您需要首先做一個SDK版本檢查:

if (android.os.Build.VERSION.SDK_INT < 19) { 
    mWebView.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);"); 
} else { 
    mWebView.evaluateJavascript("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);",null); 
} 
+0

您有試過嗎? @Neoh –

+0

非常感謝! – DmitryKanunnikoff

+0

嘗試過但未能正常工作 [INFO:CONSOLE(1)]「Uncaught ReferenceError:MathJax is not defined」,來源:(1) – frost

1
if (android.os.Build.VERSION.SDK_INT < 19) { 
        v.loadUrl(url); 
       } else { 
        v.evaluateJavascript(url,null); 
       } 
+0

請解釋爲什麼這是一個解決方案。 –

+0

在Android 4.4 API中有一些行爲改變[link](https://developer.android.com/about/versions/android-4.4.html#Behaviors),它不允許「loadUrl」爲Kit Kat工作 –