Android: NullPointerException Working With SharedPreferences

前端 未结 3 2062
北恋
北恋 2021-01-23 21:12

Working with SharedPreferences, this activity crashes upon launching. First I\'ll post the activity code, and then I\'ll post my LogCat. Thank you so much guys, you guys are alw

3条回答
  •  日久生厌
    2021-01-23 21:23

    You have to access the shared preferences AFTER onCreate is called. Or else the context would be null :) that's why you're getting a null pointer exception

    move this line :

    SharedPreferences settings = getSharedPreferences("gBValues", 
         Context.MODE_PRIVATE);
    

    in the onCreate()

提交回复
热议问题