I am starting to program on Android, unfortunately I have a problem :)
I have a writeSettings () method in my game in which I try to save some variables so that I can use it when you want to load the last game using the readSettings () method. There are parameters like first (second) PlayerName, first (second ) PlayerScore , etc. I try to use SharedPreferences to save them, but when I start the program, I launch the "Force Close" dialog.
SharedPreferences preferences = getPreferences(MODE_PRIVATE); //Global variable private void writeSettings() { SharedPreferences.Editor editor = preferences.edit(); editor.putInt("timeSave", time); editor.putString("firstPlayerNameSave", firstPlayerNameTextView.getText().toString()); editor.putString("secondPlayerNameSave", secondPlayerNameTextView.getText().toString()); editor.putString("firstPlayerScoreSave", firstPlayerScoreTextView.getText().toString()); editor.putString("secondPlayerScoreSave", secondPlayerScoreTextView.getText().toString()); editor.putInt("nowPlayerSave", nowPlayer); editor.commit(); } private void readSettings() { //"time" parameter program reads in another method firstPlayerNameTextView.setText(preferences.getString("firstPlayerNameSave", "")); secondPlayerNameTextView.setText(preferences.getString("secondPlayerNameSave", "")); firstPlayerScoreTextView.setText(preferences.getString("firstPlayerScoreSave", "")); secondPlayerScoreTextView.setText(preferences.getString("secondPlayerScoreSave", "")); nowPlayer = preferences.getInt("nowPlayerSave", -1); }
As I could know, the problem occurs when I first write writeSettings (). But I do not know what I am doing wrong?
Sorry for my English.
update
12-23 16:23:31.334: ERROR/AndroidRuntime(410): Uncaught handler: thread main exiting due to uncaught exception 12-23 16:23:31.520: ERROR/AndroidRuntime(410): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game/com.game.Game}: java.lang.NullPointerException 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.os.Handler.dispatchMessage(Handler.java:99) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.os.Looper.loop(Looper.java:123) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread.main(ActivityThread.java:4363) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at java.lang.reflect.Method.invoke(Method.java:521) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at dalvik.system.NativeStart.main(Native Method) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): Caused by: java.lang.NullPointerException 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.Activity.getLocalClassName(Activity.java:3410) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.Activity.getPreferences(Activity.java:3444) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at com.game.Game.<init>(Game.java:69) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at java.lang.Class.newInstanceImpl(Native Method) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at java.lang.Class.newInstance(Class.java:1479) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409) 12-23 16:23:31.520: ERROR/AndroidRuntime(410): ... 11 more
Game.java:69 is SharedPreferences preferences = getPreferences(MODE_PRIVATE);
P_king
source share