Java代码
private void sharedpreferenceAl() {
SharedPreferences sp = getSharedPreferences("geri", Context.MODE_PRIVATE);
git.setText(String.valueOf(sp.getInt("salak", 1)));
}
private void sharedpreferencekaydet() {
SharedPreferences sp = getSharedPreferences("geri", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sp.edit();
prefEditor.putString("salak", git.getText().toString());
prefEditor.commit();
Log.i("tago", "aaaaaaaaaaa");
gitbak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedpreferenceAl();
}
});
LOG CAT
致命的例外:主要
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239)
at kporejct_replics.sunnetler.MainActivity.sharedpreferenceAl(MainActivity.java:123)
at kporejct_replics.sunnetler.MainActivity.access$100(MainActivity.java:23)
at kporejct_replics.sunnetler.MainActivity$3.onClick(MainActivity.java:102)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:2)
更改以下行 -
git.setText(String.valueOf(sp.getInt("salak", 1)));
到 -
git.setText(sp.getString("salak", "1"));
答案 1 :(得分:0)
您将 String
作为 salak 并将其设为 Int
只需改变
sp.getInt("salak", 1))
到
sp.getString("salak", 1)
或
只需更改为
git.setText(sp.getString("salak", 1));