getFileDir() cause NullPointer exception

前端 未结 1 678
清歌不尽
清歌不尽 2021-01-13 12:22

I\'m in my early days of learning Java but hopefully someone get explain what I\'m doing wrong here, numerous Google searches have left my head spinning.

I\'ve been

相关标签:
1条回答
  • 2021-01-13 12:53

    There's a bunch of stuff wrong here. First of all, Activity classes, or in your case AppCompatActivity classes are generally for presenting screens with UI, not performing discreet tasks. Your getSaved() function should probably just reside in whichever Activity actually houses your UI.

    Second, AppCompatActivity classes can't perform context actions statically. In other words, your Activity needs to be instantiated before you can use its super class methods, in this case getFilesDir().

    What that boils down to is that you need to initialize filename in a lifecycle function, probably onCreate()

    0 讨论(0)
提交回复
热议问题