0
當我執行該行時,我有一個NPE FileInputStream ifs=openFileInput(fileName);
我得到一個NPE。我得到同樣的NPE當我執行NullPointer異常和FileInputStream
FileInputStream ifs=getApplicationContext().openFileInput(fileName);
完整的堆棧跟蹤是在這裏。
07-25 22:00:35.852 25063-25063/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.ollien.flashcarder.MainActivity.setPagerFile(MainActivity.java:353)
at com.ollien.flashcarder.SetsFragment$1.onItemClick(SetsFragment.java:47)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1215)
at android.widget.ListView.performItemClick(ListView.java:4506)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2959)
at android.widget.AbsListView$1.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
有人可以幫忙嗎?我不太清楚爲什麼會發生這種情況。 fileName不爲空,並且等於"hi there.txt"
謝謝!
你能檢查getApplicationContext()是否返回null嗎?只是一個日誌。如果您確定文件名不爲空 – Nicholas
您是否偶然在'onItemClick'或'setPagerFile'中調用'attachBaseContext()'?我問,因爲'getApplicationContext()'和'openFileInput()'有一個調用mBaseContext.SOMETHING,所以如果你設置爲null,那麼這將解釋NPE。 – Jonathan
@Nicholas getApplicationContext();拋出一個NPE,當我嘗試System.out它 – ollien