所以,我正在学习教程#12,一切都很好。然后我尝试进入教程#13保存/恢复上次读取的位置和应用程序崩溃:
04-10 15:59:26.510: E/Trace(704): error opening trace file: No such file or directory (2)
04-10 15:59:27.491: D/AndroidRuntime(704): Shutting down VM
04-10 15:59:27.491: W/dalvikvm(704): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-10 15:59:27.500: E/AndroidRuntime(704): FATAL EXCEPTION: main
04-10 15:59:27.500: E/AndroidRuntime(704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.commonsware.empublite/com.commonsware.empublite.EmPubLiteActivity}: java.lang.NullPointerException
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.os.Looper.loop(Looper.java:137)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-10 15:59:27.500: E/AndroidRuntime(704): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 15:59:27.500: E/AndroidRuntime(704): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 15:59:27.500: E/AndroidRuntime(704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-10 15:59:27.500: E/AndroidRuntime(704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-10 15:59:27.500: E/AndroidRuntime(704): at dalvik.system.NativeStart.main(Native Method)
04-10 15:59:27.500: E/AndroidRuntime(704): Caused by: java.lang.NullPointerException
04-10 15:59:27.500: E/AndroidRuntime(704): at com.commonsware.empublite.ModelFragment.executeAsyncTask(ModelFragment.java:51)
04-10 15:59:27.500: E/AndroidRuntime(704): at com.commonsware.empublite.ModelFragment.deliverModel(ModelFragment.java:38)
04-10 15:59:27.500: E/AndroidRuntime(704): at com.commonsware.empublite.ModelFragment.onActivityCreated(ModelFragment.java:29)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.Activity.performStart(Activity.java:5018)
04-10 15:59:27.500: E/AndroidRuntime(704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
04-10 15:59:27.500: E/AndroidRuntime(704): ... 11 more
我试图注释掉我所做的代码,直到最后一点,它是工作的,但这似乎没有工作。我也不太确定如何阅读这个堆栈跟踪。我应该如何解决这个问题?
谢谢。
我不太确定如何读取这个堆栈跟踪。我应该如何解决这个问题?
ModelFragment.java的第51行,在executeAsyncTask()
方法中是您崩溃的地方(参见最后一个堆栈跟踪节,从Caused by: java.lang.NullPointerException
开始)。
你的行号和答案中的行号不完全一致。然而,executeAsyncTask()
可以有NullPointerException
的唯一方法是,如果你传入的AsyncTask
是null
,这应该是不可能的,因为你在deliverModel()
方法中调用executeAsyncTask()
之前立即定义这些任务。可能您漏掉了deliverModel()
中的一行。