Note that there are some explanatory texts on larger screens.

plurals
  1. POSharedPreferences returns a null even though it haves a default value
    primarykey
    data
    text
    <p>I have my main activity which saves and load some data from the SharedPreferences... I've been able to do this until now.</p> <p>These are my onCreate, onPause and OnResume methods.</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle(R.string.title_activity_main); this.spinner = (Spinner) findViewById(R.id.geomObject); this.sbWidth = (SeekBar) findViewById(R.id.widthSB); this.sbDepth = (SeekBar) findViewById(R.id.depthSB); this.sbHeight = (SeekBar) findViewById(R.id.heightSB); this.widthProgress = (TextView) findViewById(R.id.widthDisplay); this.depthProgress = (TextView) findViewById(R.id.depthDisplay); this.heightProgress = (TextView) findViewById(R.id.heightDisplay); ...more code... } protected void onResume() { super.onResume(); loadPrefs(); } protected void onPause() { super.onPause(); savePrefs(); } </code></pre> <p>And these are the method I do most of the work, so I can keep the onX methods clean:</p> <pre><code>private void savePrefs() { this.width = this.sbWidth.getProgress(); this.depth = this.sbDepth.getProgress(); this.height = this.sbHeight.getProgress(); this.forma = this.spinner.getSelectedItemPosition(); getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("width", width).commit(); getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("depth", depth).commit(); getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("height", height).commit(); getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("forma", forma).commit(); getSharedPreferences("LOCKBARS", MODE_PRIVATE).edit().putBoolean("lockbars", menuLock.isChecked()).commit(); } private void loadPrefs() { int savedSpinner = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("forma", SPINNER_DEFAULT); int savedWidth = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("width", SEEKBAR_DEFAULT); int savedDepth = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("depth", SEEKBAR_DEFAULT); int savedHeight = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("height", SEEKBAR_DEFAULT); boolean savedLockFlag = getSharedPreferences("LOCKBARS", MODE_PRIVATE).getBoolean("lockbars", false); this.spinner.setSelection(savedSpinner); this.sbWidth.setProgress(savedWidth); this.sbDepth.setProgress(savedDepth); this.sbHeight.setProgress(savedHeight); //This is the line raising the exception Display: this.menuLock.setChecked(savedLockFlag); this.widthProgress.setText(String.valueOf(savedWidth) + " " + getResources().getString(R.string.blocks)); this.depthProgress.setText(String.valueOf(savedDepth) + " " + getResources().getString(R.string.blocks)); this.heightProgress.setText(String.valueOf(savedHeight) + " " + getResources().getString(R.string.blocks)); } </code></pre> <p>I don't see where is the error. I've been searching and I know that I can't call getSharedPreferences on onCreate, that's why I do it on onResume. What's wrong?</p> <p>I also attach the logcat, just in case:</p> <pre><code>FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity {es.nirvash.android.msg/es.nirvash.android.msg.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2567) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2595) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2109) at android.app.ActivityThread.access$600(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4575) 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:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at es.nirvash.android.msg.MainActivity.loadPrefs(MainActivity.java:231) at es.nirvash.android.msg.MainActivity.onResume(MainActivity.java:147) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154) at android.app.Activity.performResume(Activity.java:4539) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2557) ... 12 more </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload