2011-11-21 172 views
0
public class EpubReaderMainActivity extends Activity { 
    /** Called when the activity is first created. */ 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TextView tv = (TextView)findViewById(R.id.text); 

     AssetManager am = getAssets(); 

     try{ 
      InputStream bookData = am.open("books/bookTest.epub"); 
      Book book = (new EpubReader()).readEpub(bookData);   
      Log.i("epublib", "author(s): " + book.getMetadata().getAuthors()); 
      Log.i("epublib", "title: " + book.getTitle()); 
      Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage() 
         .getInputStream()); 
      Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by " 
         + coverImage.getHeight() + " pixels"); 
      logTableOfContents(book.getTableOfContents().getTocReferences(), 0); 

     } 
     catch (IOException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
     try{ 
      // read epub file 
      EpubReader epubReader = new EpubReader(); 
      Book book = epubReader.readEpub(new FileInputStream("mybook.epub")); 

      // print the first title 
      List<String> titles = book.getMetadata().getTitles(); 
      String a = ("book title:" + (titles.isEmpty() ? "book has no title" : titles.get(0))); 
      tv.setText(a); 
     } 
     catch (IOException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

    } 

    private void logTableOfContents(List<TOCReference> tocReferences, int depth) { 
     // TODO Auto-generated method stub 
     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); 
      } 

     } 
    } 

堆棧跟蹤我測試nl.siegmann.epublib.epub和我遇到無法啓動活動ComponentInfo

11-21 10:51:19.570: I/AndroidLoggerFactory(325): Logger name 'nl.siegmann.epublib.epub.EpubReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubReader' instead. 
    11-21 10:51:19.780: D/dalvikvm(325): GC_CONCURRENT freed 1136K, 56% free 2984K/6727K, external 2002K/2137K, paused 3ms+3ms 
    11-21 10:51:19.790: I/AndroidLoggerFactory(325): Logger name 'nl.siegmann.epublib.epub.EpubProcessorSupport' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubProces*' instead. 
    11-21 10:51:19.830: I/AndroidLoggerFactory(325): Logger name 'nl.siegmann.epublib.epub.PackageDocumentReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.PackageDoc*' instead. 
    11-21 10:51:19.929: I/AndroidLoggerFactory(325): Logger name 'nl.siegmann.epublib.epub.NCXDocument' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.NCXDocument' instead. 
    11-21 10:51:19.980: I/epublib(325): author(s): [Haynes, Carrie James] 
    11-21 10:51:19.980: I/epublib(325): title: Whispers of a Legend - Part One - Shadows of the Past 
    11-21 10:51:19.980: D/AndroidRuntime(325): Shutting down VM 
    11-21 10:51:19.980: W/dalvikvm(325): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
    11-21 10:51:19.990: E/AndroidRuntime(325): FATAL EXCEPTION: main 
    11-21 10:51:19.990: E/AndroidRuntime(325): java.lang.RuntimeException: Unable to start activity ComponentInfo{epub.reader/epub.reader.EpubReaderMainActivity}: java.lang.NullPointerException 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.os.Handler.dispatchMessage(Handler.java:99) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.os.Looper.loop(Looper.java:123) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread.main(ActivityThread.java:3683) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at java.lang.reflect.Method.invokeNative(Native Method) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at java.lang.reflect.Method.invoke(Method.java:507) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at dalvik.system.NativeStart.main(Native Method) 
    11-21 10:51:19.990: E/AndroidRuntime(325): Caused by: java.lang.NullPointerException 
    11-21 10:51:19.990: E/AndroidRuntime(325): at epub.reader.EpubReaderMainActivity.onCreate(EpubReaderMainActivity.java:43) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    11-21 10:51:19.990: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
    11-21 10:51:19.990: E/AndroidRuntime(325): ... 11 more 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="epub.reader" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".EpubReaderMainActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我一直在努力了雖然,但我卡住了。 這是我試過的檢查Epublib軟件包,如果有任何權限的要求,以運行該活動。此活動執行的操作是登錄.epub文件,並在測試其代碼後閱讀第一個標題。

回答

0

研究跟蹤仔細:對底部出現了

11-21 10:51:19.990: E/AndroidRuntime(325): Caused by: java.lang.NullPointerException 
11-21 10:51:19.990: E/AndroidRuntime(325): at epub.reader.EpubReaderMainActivity.onCreate(EpubReaderMainActivity.java:43) 

檢查onCreate方法,在活動類文件第43行,無論是:)

+0

謝謝。對不起,仍然對java新來說一般沒有發現NullPointerException – wesdfgfgd

1

我解決它通過使用This。空指針異常是由於slf4和epub庫的部分加載導致的。

相關問題