Android TagHandler异常

时间:2014-11-13 15:48:30

标签: java android tag-handler

我正在开发Android应用程序,我需要将HTLM(也是< li>标记)显示到textView中。为此,我发现了这个TagHandler,但它提出了一个

  

java.lang.RuntimeException:PARAGRAPH span必须从段落开始   边界

我试图了解原因,但我无法找到解决方案。我对Android很陌生,所以请帮助我。

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();

    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
            if (opening) {
                mListParents.add(tag);
            } else mListParents.remove(tag);

            mListItemCount = 0;
        } else if (tag.equals("li") && !opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {
        if (mListParents.lastElement().equals("ul")) {
            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
        } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount++;

            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.insert(start, mListItemCount + ". ");
            output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
        }
    }
}

1 个答案:

答案 0 :(得分:7)

发现错误,只需在

之前添加此if(start<output.length())即可解决
output.setSpan()

正确且有效的代码是:

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();

    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
            if (opening) {
                mListParents.add(tag);
            } else mListParents.remove(tag);

            mListItemCount = 0;
        } else if (tag.equals("li") && !opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {
        if (mListParents.lastElement().equals("ul")) {
            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            if(start<output.length())
                output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
        } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount++;

            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.insert(start, mListItemCount + ". ");
            if(start<output.length())
               output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
        }
    }
    }

<强>更新

在上面的代码遇到其他问题之后,我已经开发出了无异常工作的代码,我希望它会有用。

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();
    private Vector<Integer> mListCounter = new Vector<Integer>();


    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") ) {
            if (opening) {
                mListParents.add(mListParents.size(),tag);
                mListCounter.add(mListCounter.size(),  0);

            } else {
                Log.d("Bohhh"," tolgo "+mListParents.get(mListParents.size()-1));
                mListParents.removeElementAt(mListParents.size()-1);
                Log.d("Bohhh","lunghezza "+mListParents.size());
                mListCounter.removeElementAt(mListCounter.size()-1);

            }

        } else if (tag.equals("li") && opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {

        if (mListParents.lastElement().equals("ul")) {
           if(output.length()!=0)
            output.append("\n");
            for(int i=1;i<mListCounter.size();i++)
                output.append("\t");
            output.append("• ");
           } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount=            mListCounter.lastElement()+1;
            if(output.length()!=0)
            output.append("\n");
            for(int i=1;i<mListCounter.size();i++)
                output.append("\t");
           output.append( mListItemCount + ". ");
           mListCounter.removeElementAt(mListCounter.size()-1);
           mListCounter.add(mListCounter.size(), mListItemCount);
      }
    }

}