我想在我的Android应用中逐页阅读epub文件。
我已经尝试但是在这一行得到了一个nullPointer异常:
Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
任何人都可以解释为什么我会收到此错误或如何逐页阅读?
这是我的活动代码:
public class LogTestBookInfo extends Activity
{
@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("assets/books/wodehouse.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());
}
}
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);
}
}
}
这是我的logcat信息:
12-04 11:12:08.606: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.EpubReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubReader' instead.
12-04 11:12:08.635: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.domain.Resource' exceeds maximum length of 23 characters, using 'n*.s*.e*.d*.Resource' instead.
12-04 11:12:08.886: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.EpubProcessorSupport' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubProces*' instead.
12-04 11:12:08.996: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.PackageDocumentReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.PackageDoc*' instead.
12-04 11:12:09.336: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.NCXDocument' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.NCXDocument' instead.
12-04 11:12:09.565: I/epublib(334): author(s): [B., Gummere, Francis]
12-04 11:12:09.565: I/epublib(334): title: Beowulf
12-04 11:12:09.606: E/AndroidRuntime(334): FATAL EXCEPTION: main
12-04 11:12:09.606: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookapp/com.bookapp.LogTestBookInfo}: java.lang.NullPointerException
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 11:12:09.606: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:12:09.606: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 11:12:09.606: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 11:12:09.606: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 11:12:09.606: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
12-04 11:12:09.606: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
12-04 11:12:09.606: E/AndroidRuntime(334): at com.bookapp.LogTestBookInfo.onCreate(LogTestBookInfo.java:80)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 11:12:09.606: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 11:12:09.606: E/AndroidRuntime(334): ... 11 more
答案 0 :(得分:0)
像这样更改代码
if(book.getCoverImage()!=null&&book.getCoverImage().getInputStream()!=null)
Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());