我正在尝试应用sharedPreferences来保存我的Android游戏中的高分。我正在尝试这个让我理解sharedPreferences的基本流程。但是,不幸的是,我在显示数据时出错。为什么我有这个错误?
09-07 21:06:51.802: E/AndroidRuntime(477): FATAL EXCEPTION: main
09-07 21:06:51.802: E/AndroidRuntime(477): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathattack/com.example.mathattack.Finish}: java.lang.NullPointerException
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.os.Looper.loop(Looper.java:123)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 21:06:51.802: E/AndroidRuntime(477): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 21:06:51.802: E/AndroidRuntime(477): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 21:06:51.802: E/AndroidRuntime(477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 21:06:51.802: E/AndroidRuntime(477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 21:06:51.802: E/AndroidRuntime(477): at dalvik.system.NativeStart.main(Native Method)
09-07 21:06:51.802: E/AndroidRuntime(477): Caused by: java.lang.NullPointerException
09-07 21:06:51.802: E/AndroidRuntime(477): at com.example.mathattack.Finish.onCreate(Finish.java:67)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 21:06:51.802: E/AndroidRuntime(477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 21:06:51.802: E/AndroidRuntime(477): ... 11 more
这是我的code snippet
:
public class Finish extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.finish);
Button okay = (Button) findViewById (R.id.okay);
Intent svc=new Intent(getApplicationContext(), MusicService.class);
stopService(svc);
TextView Set1 = (TextView) findViewById (R.id.time1);
TextView totalv = (TextView) findViewById (R.id.total);
TextView nym = (TextView) findViewById (R.id.name);
long lvl1 = getIntent().getLongExtra("time1", 0L);
long lvl2 = getIntent().getLongExtra("time2", 0L);
long lvl3 = getIntent().getLongExtra("time3", 0L);
long time1 = lvl1;
int hours = (int) (time1 / 3600000);
int minutes1 = (int) (time1 - hours * 3600000) / 60000;
int seconds1 = (int) (time1 - hours * 3600000 - minutes1 * 60000) / 1000;
long time2 = lvl2;
int hours2 = (int) (time1 / 3600000);
int minutes2 = (int) (time2 - hours2 * 3600000) / 60000;
int seconds2 = (int) (time2 - hours2 * 3600000 - minutes2 * 60000) / 1000;
long time3 = lvl3;
int hours3 = (int) (time3 / 3600000);
int minutes3 = (int) (time3 - hours3 * 3600000) / 60000;
int seconds3 = (int) (time3 - hours3 * 3600000 - minutes3 * 60000) / 1000;
long total = time1 + time2 + time3;
int totalH = (int) (total / 3600000);
int totalMin = (int) (total - totalH * 3600000) / 60000;
int totalSec = (int) (total - totalH * 3600000 - totalMin * 60000) / 1000;
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("score", totalSec);
prefsEditor.commit();
SharedPreferences myPrefs2 = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
int highestScore = myPrefs2.getInt("score", 0);
Set1.setText("Lvl1 Time: " + minutes1 + ":" + seconds1 + "\n" + "Lvl2 Time: " + minutes2 + ":" + seconds2 + "\n" +"Lvl3 Time: " + minutes3+ ":" + seconds3);
totalv.setText("Total time: " + totalMin + ":" + totalSec);
nym.setText("Score" + highestScore);