高分节省工作但检索错误

时间:2014-03-17 11:49:51

标签: android sharedpreferences

以下代码正在保存高分:

SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
TextView outputView = (TextView) findViewById(R.id.textscore);
CharSequence textData = outputView.getText();


 if (textData != null && !textData.toString().trim().equals("")) {
       int score1 = Integer.parseInt(textData.toString().trim());
       if(score1 > prefs.getInt(TEXT_DATA_KEY, 0)) 
       {                       
           editor.putInt(TEXT_DATA_KEY, score1);
           editor.commit();

       }

    }

这是阅读代码,但是当我尝试运行此代码时,它会崩溃

SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");
TextView outputView = (TextView) findViewById(R.id.textread); 
outputView.setText(textData); 

这是logcat - 当我尝试在崩溃发生后检索存储的代码

  03-17 19:44:43.777: E/AndroidRuntime(7949): FATAL EXCEPTION: main
  03-17 19:44:43.777: E/AndroidRuntime(7949): java.lang.ClassCastException:   java.lang.Integer cannot be cast to java.lang.String
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at com.example.dip.App2Activity$2.onClick(App2Activity.java:160)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.view.View.performClick(View.java:3530)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.view.View$PerformClick.run(View.java:14201)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.os.Handler.handleCallback(Handler.java:605)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.os.Handler.dispatchMessage(Handler.java:92)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.os.Looper.loop(Looper.java:137)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at android.app.ActivityThread.main(ActivityThread.java:4519)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at java.lang.reflect.Method.invokeNative(Native Method)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at java.lang.reflect.Method.invoke(Method.java:511)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
  03-17 19:44:43.777: E/AndroidRuntime(7949):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您将int添加到SharedPreferences,但尝试获取String。由于SP的架构如下:

<map>
  <int name="id" value="1" />
  <string name="first">John</string>
</map>

你应该替换它:

String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");
TextView outputView = (TextView) findViewById(R.id.textread); 
outputView.setText(textData);

到此:

int data = prefs.getInt(TEXT_DATA_KEY, 0);
String textData = (data == 0) ? "No Preferences!" : String.valueOf(data);
TextView outputView = (TextView) findViewById(R.id.textread); 
outputView.setText(textData); 

答案 1 :(得分:1)

您正在保存整数

editor.putInt(TEXT_DATA_KEY, score1);

阅读字符串

String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");

你得到ClassCastException。无论如何使用

int score = prefs.getInt(TEXT_DATA_KEY, DEFAULT_VALUE);