SharedPreferences에서 일부 데이터를 저장하고로드하는 주요 활동이 있습니다. 지금까지 이것을 수행 할 수있었습니다.SharedPreferences가 기본값을 갖더라도 null을 반환합니다.
이들은 내 onCreate, onPause 및 OnResume 메소드입니다.
@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();
}
는 그리고이 나는 대부분의 작업을 수행하는 방법이다, 그래서 나는 깨끗하게 onX 방법을 유지할 수 있습니다 : 오류 어디가 표시되지 않습니다
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));
}
. 나는 검색 중이며 onCreate에서 getSharedPreferences를 호출 할 수 없다는 것을 알고 있으므로 onResume에서이 작업을 수행합니다. 뭐가 문제 야?
은 또한 단지 경우, 로그 캣 첨부 :
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
"어디서 오류인지 알 수 없습니다. 검색 중이며 onCreate에서 getSharedPreferences를 호출 할 수 없다는 것을 알고 있습니다. 따라서 onResume에서이 작업을 수행하는 이유는 무엇입니까?" 그건 틀렸어요. – dymmeh
여기 : http://stackoverflow.com/a/5991458/1569084 "getSharedPreferences()는 onCreate()가 Activity에서 호출 된 후에 만 호출 할 수 있습니다." – Deses
디버거를 사용하십시오. 정확하게 null인지 확인하십시오. – njzk2