Mathjax离线应用程序错误

时间:2014-09-18 11:08:49

标签: android mathjax

您好我正在尝试离线实现mathjax,我在github上提到这个https://github.com/leathrum/android-apps,Can任何人都有帮助,为什么这不起作用? 我已尝试从每个样本我可以上网,目前在其他应用程序,它适用于在线时添加

  <html><head><script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script><link href=\"main.css\"type=\"text/css\" rel=\"stylesheet\" /></head><body>

试过这件事......

public class MainActivity extends Activity implements OnClickListener{

    private String doubleEscapeTeX(String s)
    {
        String s1 = "";
        int i = 0;
        do
        {
            if (i >= s.length())
            {
                return s1;
            }
            if (s.charAt(i) == '\'')
            {
                StringBuffer stringbuffer = new StringBuffer();
                s1 = stringbuffer.append(s1).append('\\').toString();
            }
            if (s.charAt(i) != '\n')
            {
                StringBuffer stringbuffer1 = new StringBuffer();
                s1 = stringbuffer1.append(s1).append(s.charAt(i)).toString();
            }
            if (s.charAt(i) == '\\')
            {
                StringBuffer stringbuffer2 = new StringBuffer();
                s1 = stringbuffer2.append(s1).append("\\").toString();
            }
            i++;
        } while (true);
    }

    private int exampleIndex = 0;

    private String getExample(int index) {
        return getResources().getStringArray(R.array.tex_examples)[index];
    }



    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        WebView webview = (WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(true);
        StringBuffer stringbuffer = new StringBuffer();
        StringBuffer stringbuffer1 = new StringBuffer();
        StringBuffer stringbuffer2 = new StringBuffer();
        StringBuffer stringbuffer3 = new StringBuffer();
        StringBuffer stringbuffer4 = new StringBuffer();
        StringBuffer stringbuffer5 = new StringBuffer();
        StringBuffer stringbuffer6 = new StringBuffer();
        StringBuffer stringbuffer7 = new StringBuffer();
        StringBuffer stringbuffer8 = new StringBuffer();
        webview.loadDataWithBaseURL("http://bar", stringbuffer.append(stringbuffer1.append(stringbuffer2.append(stringbuffer3.append(stringbuffer4.append(stringbuffer5.append(stringbuffer6.append(stringbuffer7.append(stringbuffer8.append("<script type='text/x-mathjax-config'>").append("MathJax.Hub.Config({ ").toString()).append("showMathMenu: false, ").toString()).append("jax: ['input/TeX','output/HTML-CSS'], ").toString()).append("extensions: ['tex2jax.js'], ").toString()).append("TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'] } ").toString()).append("});</script>").toString()).append("<script type='text/javascript' ").toString()).append("src='file:///android_asset/MathJax/MathJax.js'").toString()).append("></script><span id='math'></span>").toString(), "text/html", "utf-8", "");
        EditText edittext = (EditText)findViewById(R.id.edit);
        edittext.setBackgroundColor(Color.LTGRAY);
        edittext.setTextColor(Color.BLACK);
        edittext.setText("");
        ((Button)findViewById(R.id.button2)).setOnClickListener(this);
        ((Button)findViewById(R.id.button3)).setOnClickListener(this);
        ((Button)findViewById(R.id.button4)).setOnClickListener(this);
        TextView textview = (TextView)findViewById(R.id.textview3);
        textview.setMovementMethod(LinkMovementMethod.getInstance());
        textview.setText(Html.fromHtml(textview.getText().toString()));
    }

    public void onClick(View view)
    {
        if (view == findViewById(R.id.button2))
        {
            WebView webview2 = (WebView)findViewById(R.id.webview);
            EditText edittext1 = (EditText)findViewById(R.id.edit);
            StringBuffer stringbuffer2 = new StringBuffer();
            StringBuffer stringbuffer3 = new StringBuffer();
            webview2.loadUrl(stringbuffer2.append(stringbuffer3.append("javascript:document.getElementById('math').innerHTML='\\\\[").append(doubleEscapeTeX(edittext1.getText().toString())).toString()).append("\\\\]';").toString());
            webview2.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
        } else
        {
            if (view == findViewById(R.id.button3))
            {
                WebView webview1 = (WebView)findViewById(R.id.webview);
                ((EditText)findViewById(R.id.edit)).setText("");
                webview1.loadUrl("javascript:document.getElementById('math').innerHTML='';");
                webview1.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
                return;
            }
            if (view == findViewById(R.id.button4))
            {
                WebView webview = (WebView)findViewById(R.id.webview);
                EditText edittext = (EditText)findViewById(R.id.edit);
                int i = exampleIndex;
                exampleIndex = i + 1;
                edittext.setText(getExample(i));
                if (exampleIndex > -1 + getResources().getStringArray(R.array.tex_examples).length)
                {
                    exampleIndex = 0;
                }
                StringBuffer stringbuffer = new StringBuffer();
                StringBuffer stringbuffer1 = new StringBuffer();
                webview.loadUrl(stringbuffer.append(stringbuffer1.append("javascript:document.getElementById('math').innerHTML='\\\\[").append(doubleEscapeTeX(edittext.getText().toString())).toString()).append("\\\\]';").toString());
                webview.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
                return;
            }
        }
    }
}

得到这样的错误

   E/AndroidProtocolHandler(26612): Unable to open asset URL: file:///android_asset/MathJax/MathJax.js
   I/chromium(26612): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
   I/chromium(26612): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
   D/dalvikvm(26612): GC_FOR_ALLOC freed 92K, 12% free 9425K/10596K, paused 14ms, total 14ms
   I/chromium(26612): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)

   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'innerHTML' of null", source: http://bar/ (1)
   I/chromium(27592): [INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source: http://bar/ (1)

1 个答案:

答案 0 :(得分:0)

无需使用StringBuffer,只需尝试添加以下行

 WebView w = (WebView) findViewById(R.id.webview);
    w.getSettings().setJavaScriptEnabled(true);
    w.getSettings().setBuiltInZoomControls(true);
    w.loadDataWithBaseURL("http://bar/", "<script type='text/x-mathjax-config'>"
                          +"MathJax.Hub.Config({ " 
                            +"showMathMenu: false, "
                            +"jax: ['input/TeX','output/HTML-CSS'], " // output/SVG
                            +"extensions: ['tex2jax.js','toMathML.js'], " 
                            +"TeX: { extensions: ['AMSmath.js','AMSsymbols.js',"
                              +"'noErrors.js','noUndefined.js'] }, "
                          +"});</script>"
                          +"<script type='text/javascript' "
                          +"src='file:///android_asset/MathJax/MathJax.js'"
                          +"></script>"
                          +"<script type='text/javascript'>getLiteralMML = function() {"
                          +"math=MathJax.Hub.getAllJax('math')[0];"
                          +"mml=math.root.toMathML(''); return mml;"
                          +"}; getEscapedMML = function() {"
                          +"math=MathJax.Hub.getAllJax('math')[0];"
                          +"mml=math.root.toMathMLquote(getLiteralMML()); return mml;}"
                          +"</script>"
                          +"<span id='math'></span><pre><span id='mmlout'></span></pre>","text/html","utf-8","");
    w.addJavascriptInterface(new Object() { public void clipMML(String s) {
                WebView ww = (WebView) findViewById(R.id.webview);  
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText(s);
                Toast.makeText(getApplicationContext(),"MathML copied to clipboard",Toast.LENGTH_SHORT).show();
        }}, "injectedObject");