0
我正在开发打开epub文件的图书阅读器。我在因特网上搜索了很多,最后在此tutorial link中找到了解决方案。Android(Java) - 通过递归方法填充ArrayList
但现在我遇到的麻烦与方法
logTableOfContents(名单tocReferences,诠释深度)
我mofided从无效到字符串,它是我的代码
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;
}
可以清楚地看到,编写该教程的开发人员已经使用递归方法来获得目录。
我改变方法无效为String获取并存储在ArrayList中 ,但是试图登录ArrayList中的元素时,不能单独获得的内容本书的表外递归method.Got 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 android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.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);
}
}
}
,我用的教程已经写库。