我需要将语法高亮显示集成到我的Android应用程序中。我尝试使用Java Prettify(https://code.google.com/p/java-prettify),但无法集成它。该应用程序崩溃了。而且由于我的avd无法正常工作,因此我无法从logcat获取有关崩溃的信息。

我正在关注以下信息:https://code.google.com/p/java-prettify/wiki/UseTheParserAlone

到目前为止,我的代码:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import prettify.PrettifyParser;
import syntaxhighlight.ParseResult;
import syntaxhighlight.Parser;

public class PrettifyHighlighter {
Map<String, String> COLORS = buildColorsMap();
String FONT_PATTERN = "<font color=\"#%s\">%s</font>";
Parser parser = new PrettifyParser();

public String highlight(String fileExtension, String sourceCode) {
    StringBuilder highlighted = new StringBuilder();
    List<ParseResult> results = parser.parse(fileExtension, sourceCode);
    for(ParseResult result : results){
        String type = result.getStyleKeys().get(0);
        String content = sourceCode.substring(result.getOffset(), result.getOffset() + result.getLength());
        highlighted.append(String.format(FONT_PATTERN, getColor(type), content));
    }
    return highlighted.toString();
}

private String getColor(String type){
    return COLORS.containsKey(type) ? COLORS.get(type) : COLORS.get("pln");
}

private static Map<String, String> buildColorsMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("typ", "87cefa");
    map.put("kwd", "00ff00");
    map.put("lit", "ffff00");
    map.put("com", "999999");
    map.put("str", "ff4500");
    map.put("pun", "eeeeee");
    map.put("pln", "ffffff");
    return map;
}


}

在调试时,我发现在注释该行时:
    解析器parser = new PrettifyParser();

而且是相关的,没有崩溃的应用程序。
我用谷歌搜索,但没有找到解决方案。

我的logcat:

03-02 07:57:12.300: E/AndroidRuntime(1245): FATAL EXCEPTION: main
03-02 07:57:12.300: E/AndroidRuntime(1245): Process: com.himanshujaju.codeit, PID: 1245
03-02 07:57:12.300: E/AndroidRuntime(1245): java.lang.NoClassDefFoundError: prettify.PrettifyParser
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.PrettifyHighlighter.<init>(PrettifyHighlighter.java:14)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.codeArea$Run.onPostExecute(codeArea.java:193)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.codeArea$Run.onPostExecute(codeArea.java:1)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.Looper.loop(Looper.java:137)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invoke(Method.java:515)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

我尝试了您的代码,效果很好。

  PrettifyHighlighter highlighter = new PrettifyHighlighter();
  String code = "for (int i = 0; i < 5; i++){System.out.println(i);}";
  String highlighted = highlighter.highlight("java", code);
  textView1.setText(Html.fromHtml(highlighted));


这是模拟器上的输出Textview



对于NoClassDefFoundError异常,看起来eclipse在构建时不会导出jar库。尝试this answer

08-26 09:32