有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android(Java)通过递归方法填充ArrayList

我正在开发可以打开epub文件的图书阅读器。我在互联网上搜索了很多,最后在这个tutorial link中找到了一个解决方案


但是现在我在使用这种方法时遇到了麻烦

内容的日志表(列表引用、int深度)

我把它从void改为String,这是我的代码

 private String logTableOfContents(List<TOCReference> tocReferences, int depth) {

    if (tocReferences == null) {




        return null;
    }

    for (TOCReference tocReference : tocReferences) {
        StringBuilder tocString = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            tocString.append("\t");
        }

        tocString.append(tocReference.getTitle());

        //Toast.makeText(this, ""+ logTableOfContents(tocReference.getChildren(), depth + 1), Toast.LENGTH_SHORT).show();

        return tocString.toString();

    }

    return null;
}

可以清楚地看到,编写该教程的开发人员使用了递归方法来获取目录


我将方法void更改为String,以获取并存储在ArrayList中 然而,无法从递归方法中单独获取该书的目录。尝试记录ArrayList的元素时出现nullPointerException


现在我想从某人那里得到帮助来解决这个问题,欢迎任何建议,提前谢谢


来自我使用的tutorial的代码

import java.io.IOException; import java.io.InputStream; import java.util.List; import nl.siegmann.epublib.domain.Book; import nl.siegmann.epublib.domain.TOCReference; import nl.siegmann.epublib.epub.EpubReader; import 安卓.app.Activity; import 安卓.content.res.AssetManager; import 安卓.graphics.Bitmap; import 安卓.graphics.BitmapFactory; import 安卓.os.Bundle; import 安卓.util.Log; /** * Log the info of 'assets/books/testbook.epub'. * * @author paul.siegmann * */ public class LogTestBookInfo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AssetManager assetManager = getAssets(); try { // find InputStream for book InputStream epubInputStream = assetManager .open("books/testbook.epub"); // Load Book from inputStream Book book = (new EpubReader()).readEpub(epubInputStream); // Log the book's authors Log.i("epublib", "author(s): " + book.getMetadata().getAuthors()); // Log the book's title Log.i("epublib", "title: " + book.getTitle()); // Log the book's coverimage property Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage() .getInputStream()); Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by " + coverImage.getHeight() + " pixels"); // Log the tale of contents logTableOfContents(book.getTableOfContents().getTocReferences(), 0); } catch (IOException e) { Log.e("epublib", e.getMessage()); } } /** * Recursively Log the Table of Contents * * @param tocReferences * @param depth */ private void logTableOfContents(List<TOCReference> tocReferences, int depth) { if (tocReferences == null) { return; } for (TOCReference tocReference : tocReferences) { StringBuilder tocString = new StringBuilder(); for (int i = 0; i < depth; i++) { tocString.append("\t"); } tocString.append(tocReference.getTitle()); Log.i("epublib", tocString.toString()); logTableOfContents(tocReference.getChildren(), depth + 1); } } }

我使用的库已在教程中编写


共 (1) 个答案

  1. # 1 楼答案

    我刚刚找到了解决我问题的办法


    我在我的logTableOfContent(…,…,ArrayList…)中添加了一个新参数方法并对其进行了修改:


    &13; 第13部分,;
    private void logTableOfContents(List<TOCReference> tocReferences, int depth, ArrayList<String> ListOfContents) {
    
            if (tocReferences == null) {
    
    
                return;
            }
    
            for (TOCReference tocReference : tocReferences) {
                StringBuilder tocString = new StringBuilder();
                for (int i = 0; i < depth; i++) {
                    tocString.append("\t");
                }
    
                tocString.append(tocReference.getTitle());
    
                Log.v("TAG", tocString.toString());
    
                ListOfContents.add(tocString.toString());
    
                logTableOfContents(tocReference.getChildren(), depth + 1, ListOfContents);
    
                //return tocString.toString();
    
            }
    
        }
    和#13;
    和#13;

    void方法可以用作

     ArrayList<String> TableOfContent = new ArrayList<>();
    
            logTableOfContents(book.getTableOfContents().getTocReferences(), 0, TableOfContent);
    
            for (String string:TableOfContent){
    
                Toast.makeText(this, ""+ string, Toast.LENGTH_SHORT).show();
    
            }
    

    感谢那些抽出时间来回顾这个问题的人