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
public class MainActivity extends Activity{
SharedPreferences sharedpreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedpreferences = getSharedPreferences("preferences", Context.MODE_PRIVATE);
editor.putBoolean("sharedpreferences", true);
editor.commit();
// your code ...
}
// your code ...
}
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()
Move the instantiation of the settings instance variable into your onCreate method. After the super.onCreate call.
The way you are doing it now, it is getting set to null;