我正在尝试阅读教程http://www.siegmann.nl/epublib/android和其他堆栈溢出链接之后的epub文件。但我仍然得到空指针异常。
我已下载这些jar,放在libs文件夹中并添加到构建路径:
1)epub-lib-core-latest.jar
2)slf4j-android-1.6.1-RC1.jar
3)slf4j-api.jar.zip
以下是例外情况:
01-12 13:11:39.968: W/dalvikvm(27889): threadid=1: thread exiting with uncaught exception (group=0x41712700)
01-12 13:11:39.968: E/AndroidRuntime(27889): FATAL EXCEPTION: main
01-12 13:11:39.968: E/AndroidRuntime(27889): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bookreaderwithlogger/com.example.bookreaderwithlogger.MainActivity}: java.lang.NullPointerException
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.os.Looper.loop(Looper.java:137)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-12 13:11:39.968: E/AndroidRuntime(27889): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 13:11:39.968: E/AndroidRuntime(27889): at java.lang.reflect.Method.invoke(Method.java:525)
01-12 13:11:39.968: E/AndroidRuntime(27889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-12 13:11:39.968: E/AndroidRuntime(27889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 13:11:39.968: E/AndroidRuntime(27889): at dalvik.system.NativeStart.main(Native Method)
01-12 13:11:39.968: E/AndroidRuntime(27889): Caused by: java.lang.NullPointerException
01-12 13:11:39.968: E/AndroidRuntime(27889): at com.example.bookreaderwithlogger.MainActivity.onCreate(MainActivity.java:37)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.Activity.performCreate(Activity.java:5133)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-12 13:11:39.968: E/AndroidRuntime(27889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-12 13:11:39.968: E/AndroidRuntime(27889): ... 11 more
01-12 13:16:40.321: I/Process(27889): Sending signal. PID: 27889 SIG: 9
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager assetManager = getAssets();
try{
//find input Stream for book
InputStream epubInputStream = assetManager.open("oliver_twist.epub");
//Load book from input stream
Book book = (new EpubReader()).readEpub(epubInputStream);
Log.i("epublib", "title: "+book.getTitle());
//Log the book's cover image property
Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
Log.i("epublib", "CoverImage is" + coverImage.getWidth()+" by "+coverImage.getHeight()+" pixels");
//Log the tales 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("epulib", tocString.toString());
logTableOfContents(tocReference.getChildren(), depth+1);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}