当我执行第FileInputStream ifs=openFileInput(fileName);
行时,我有一个NPE,我得到了一个NPE。我执行
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不为null,等于"hi there.txt"
谢谢!
答案 0 :(得分:1)
解决方案:
确保你的Context不为null,我通过函数参数传递它,例如
public void setPagerFile(String fileName,Context mContext){
然后使用mContext作为上下文。