使用简单HTMLserializer的NullPointerException

时间:2013-01-21 21:45:09

标签: java android html html-parsing htmlcleaner


我试图从TagNode解析html。 问题是,有一个内部异常阻止它工作。 这是我的电话:

CleanerProperties props = new CleanerProperties();
SimpleHtmlSerializer serializer = new SimpleHtmlSerializer(props);
changes.setHtmlForTimetable(serializer.getAsString(root));

root当然不是null,显然序列化器也不是null。

这是堆栈跟踪:

01-21 23:42:50.860: W/System.err(25196): java.lang.NullPointerException
01-21 23:42:50.865: W/System.err(25196):    at org.htmlcleaner.HtmlSerializer.isMinimizedTagSyntax(HtmlSerializer.java:54)
01-21 23:42:50.875: W/System.err(25196):    at org.htmlcleaner.HtmlSerializer.serializeOpenTag(HtmlSerializer.java:189)
01-21 23:42:50.880: W/System.err(25196):    at org.htmlcleaner.SimpleHtmlSerializer.serialize(SimpleHtmlSerializer.java:52)
01-21 23:42:50.885: W/System.err(25196):    at org.htmlcleaner.Serializer.write(Serializer.java:249)
01-21 23:42:50.890: W/System.err(25196):    at org.htmlcleaner.Serializer.getAsString(Serializer.java:176)
01-21 23:42:50.900: W/System.err(25196):    at org.htmlcleaner.Serializer.getAsString(Serializer.java:197)
01-21 23:42:50.905: W/System.err(25196):    at org.htmlcleaner.Serializer.getAsString(Serializer.java:206)
01-21 23:42:50.915: W/System.err(25196):    at com.roneven.blich.GetChanges$1$1$1.callback(GetChanges.java:125)
01-21 23:42:50.920: W/System.err(25196):    at com.roneven.blich.GetChanges$1$1$1.callback(GetChanges.java:1)
01-21 23:42:50.930: W/System.err(25196):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:501)
01-21 23:42:50.935: W/System.err(25196):    at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1269)
01-21 23:42:50.940: W/System.err(25196):    at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:993)
01-21 23:42:50.945: W/System.err(25196):    at android.os.Handler.handleCallback(Handler.java:725)
01-21 23:42:50.945: W/System.err(25196):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 23:42:50.950: W/System.err(25196):    at android.os.Looper.loop(Looper.java:137)
01-21 23:42:50.950: W/System.err(25196):    at android.app.ActivityThread.main(ActivityThread.java:5191)
01-21 23:42:50.955: W/System.err(25196):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:42:50.955: W/System.err(25196):    at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:42:50.960: W/System.err(25196):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-21 23:42:50.960: W/System.err(25196):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-21 23:42:50.965: W/System.err(25196):    at dalvik.system.NativeStart.main(Native Method)

更新: 我找到了一个解决方法,甚至没有使用HtmlSerializer或TagNode(使用html我已经成功提取了一些字符串命令)

2 个答案:

答案 0 :(得分:1)

根据HtmlCleaner的来源,HtmlSerializer.isMinimizedTagSyntax()来电CleanerProperties.getTagInfoProvider()。并在CleanerProperties.java

ITagInfoProvider tagInfoProvider = null;
...
public ITagInfoProvider getTagInfoProvider() {
    return tagInfoProvider;
}

没有办法在CleanerProperties中设置它。在使用它之前,必须以某种方式设置tagInfoProvider

我认为,要了解具体方法,您必须深入了解HtmlCleaner的文档,例如Java code usage提供的文档。

答案 1 :(得分:0)

我遇到了同样的问题,花了很多时间寻找解决方案。

CleanerProperties props = new CleanerProperties() {

    @Override
    public ITagInfoProvider getTagInfoProvider() {
        return DefaultTagProvider.getInstance();
    }

};

HtmlSerializer simpleHtmlSerializer = new SimpleHtmlSerializer(props);
String message = simpleHtmlSerializer.getAsString(tagNode, true);