不幸的是应用已经停止[阅读EPub文件]

时间:2015-06-03 01:44:30

标签: java android epub

当我在eclipse中运行我的项目时,我收到此错误:

unfortunately app has stopped

我无法运行我的项目。请帮我解决这个问题。

这个应用程序简单阅读.epub书籍文件我usd

http://www.siegmann.nl/epublib/android

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = (WebView) findViewById(R.id.webView);

    nl.siegmann.epublib.domain.Book book=null;
    try {
        EpubReader epubReader = new EpubReader();
        book = epubReader.readEpub(new FileInputStream("/book/test.epub"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String baseUrl = "/book/";
    String data=null;
    try {
        data = new String(book.getContents().get(1).getData());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    webView.loadDataWithBaseURL(baseUrl, data, "text/html", "UTF-8", null);




}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GeneralLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.EBSTePub.MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:padding="0dp"  />

LogCat [更新]

06-03 04:50:04.340: W/ApplicationPackageManager(17855): getCSCPackageItemText()
06-03 04:50:04.340: I/PersonaManager(17855): getPersonaService() name persona_policy
06-03 04:50:04.405: E/MoreInfoHPW_ViewGroup(17855): Parent view is not a TextView
06-03 04:50:04.435: V/WebViewChromium(17855): Binding Chromium to the main looper Looper (main, tid 1) {42931cb0}
06-03 04:50:04.435: I/chromium(17855): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
06-03 04:50:04.440: I/BrowserProcessMain(17855): Initializing chromium process, renderers=0
06-03 04:50:04.455: W/chromium(17855): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
06-03 04:50:04.460: W/ApplicationPackageManager(17855): getCSCPackageItemText()
06-03 04:50:04.470: I/(17855): PLATFORM VERSION : JB-MR-2
06-03 04:50:04.540: I/AndroidLoggerFactory(17855): Logger name 'nl.siegmann.epublib.epub.EpubReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubReader' instead.
06-03 04:50:04.565: I/AndroidLoggerFactory(17855): Logger name 'nl.siegmann.epublib.domain.Resource' exceeds maximum length of 23 characters, using 'n*.s*.e*.d*.Resource' instead.
06-03 04:50:04.640: D/dalvikvm(17855): GC_FOR_ALLOC freed 1302K, 15% free 18156K/21336K, paused 21ms, total 21ms
06-03 04:50:04.700: D/dalvikvm(17855): GC_FOR_ALLOC freed 2042K, 19% free 18667K/22872K, paused 13ms, total 13ms
06-03 04:50:04.795: D/dalvikvm(17855): GC_FOR_ALLOC freed 2637K, 20% free 19680K/24352K, paused 11ms, total 11ms
06-03 04:50:04.850: I/AndroidLoggerFactory(17855): Logger name 'nl.siegmann.epublib.epub.PackageDocumentReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.PackageDoc*' instead.
06-03 04:50:04.850: I/AndroidLoggerFactory(17855): Logger name 'nl.siegmann.epublib.epub.EpubProcessorSupport' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubProces*' instead.
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855): null
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855): java.lang.NullPointerException
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at nl.siegmann.epublib.epub.PackageDocumentReader.read(PackageDocumentReader.java:51)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at nl.siegmann.epublib.epub.EpubReader.processPackageResource(EpubReader.java:119)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:97)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:53)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:37)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at com.EBSTePub.MainActivity.onCreate(MainActivity.java:31)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.Activity.performCreate(Activity.java:5389)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.os.Looper.loop(Looper.java:157)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at android.app.ActivityThread.main(ActivityThread.java:5335)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at java.lang.reflect.Method.invoke(Method.java:515)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-03 04:50:04.855: E/n*.s*.e*.e*.EpubReader(17855):    at dalvik.system.NativeStart.main(Native Method)
06-03 04:50:04.855: I/AndroidLoggerFactory(17855): Logger name 'nl.siegmann.epublib.epub.NCXDocument' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.NCXDocument' instead.
06-03 04:50:04.855: E/n*.s*.e*.e*.NCXDocument(17855): Book does not contain a table of contents file
06-03 04:50:04.855: D/AndroidRuntime(17855): Shutting down VM
06-03 04:50:04.855: W/dalvikvm(17855): threadid=1: thread exiting with uncaught exception (group=0x41708c08)
06-03 04:50:04.855: E/AndroidRuntime(17855): FATAL EXCEPTION: main
06-03 04:50:04.855: E/AndroidRuntime(17855): Process: com.EBSTePub, PID: 17855
06-03 04:50:04.855: E/AndroidRuntime(17855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.EBSTePub/com.EBSTePub.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.os.Looper.loop(Looper.java:157)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread.main(ActivityThread.java:5335)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at java.lang.reflect.Method.invoke(Method.java:515)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at dalvik.system.NativeStart.main(Native Method)
06-03 04:50:04.855: E/AndroidRuntime(17855): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
06-03 04:50:04.855: E/AndroidRuntime(17855):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at java.util.ArrayList.get(ArrayList.java:308)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at com.EBSTePub.MainActivity.onCreate(MainActivity.java:43)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.Activity.performCreate(Activity.java:5389)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-03 04:50:04.855: E/AndroidRuntime(17855):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
06-03 04:50:04.855: E/AndroidRuntime(17855):    ... 11 more
06-03 04:50:07.320: I/Process(17855): Sending signal. PID: 17855 SIG: 9

注意:我的模拟器是我的手机[注3]

1 个答案:

答案 0 :(得分:0)

如果你的epub文件在assets文件夹中,那么试试这个

...
nl.siegmann.epublib.domain.Book book=null;
    try {
        AssetManager assetManager = getAssets();
        AssetFileDescriptor assetFileDescriptor = assetManager.openFd("book/test.epub");  
        FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();  
        FileInputStream stream = new FileInputStream(fileDescriptor);
        EpubReader epubReader = new EpubReader();
        book = epubReader.readEpub(stream);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ...