gpt4 book ai didi

java - Android TagHandler 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:06 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我需要将 HTLM(也是

  • 标签)显示到一个 textView 中。为此,我找到了这个 TagHandler但它提出了一个

    java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary

    我试图了解原因,但一直未能找到解决方案。我是 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);
    }
    }
    }

  • 最佳答案

    发现错误,添加这个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);
    }
    }
    }

    更新:

    在使用上面的代码遇到一些其他问题后,我开发了这段代码,它无一异常(exception)地工作,我希望它会有用。

    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);
    }
    }

    }

    关于java - Android TagHandler 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912837/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com