我创建了一个应用程序,按下按钮会将其整数值从0增加1.(它显示在textView中) 我要做的是保存该int变量,以便在重新启动应用程序后保留修改后的值。 我试图使用这些方法,将它们放在onClick(增加)方法中以及onCreate中,但似乎没有任何效果:
SharedPreferences mPrefs = getSharedPreferences("label", 0);
Integer var = mPrefs.getInt("var", variable);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putInt("tag", variable).commit();
还有这个:
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
SharedPreferences.Editor editor= sharedPref.edit();
editor.putInt("name", variable);
editor.commit();
getSharedPreferences("name", variable);
退出应用程序并启动它后,该值将重置为0
这是完整的代码:
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class TestApp extends ActionBarActivity {
public int variable = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
TextView statnumber = (TextView) findViewById(R.id.number);
statnumber.setText(String.valueOf(variable));
SharedPreferences mPrefs = getSharedPreferences("var", 0);
Integer var = mPrefs.getInt("var", variable);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putInt("var", variable).commit();
}
public void increase(View view){
TextView statnumber = (TextView) findViewById(R.id.number);
statnumber.setText(String.valueOf(variable = variable + 1));
}
}
这是logcat所说的(可能并不重要......):
05-02 22:23:53.010 10827-10827/com.rpd.testapp D/OpenGLRenderer﹕ Enabling debug mode 0
05-02 22:24:56.200 11058-11058/com.asd.testapp D/dalvikvm﹕ Late-enabling CheckJNI
05-02 22:24:56.440 11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapUtilization:0.25
05-02 22:24:56.450 11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapIdealFree:8388608
05-02 22:24:56.450 11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
05-02 22:24:56.730 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
05-02 22:24:56.810 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12212: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
05-02 22:24:56.810 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
05-02 22:24:56.810 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
05-02 22:24:56.810 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12218: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
05-02 22:24:56.810 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
05-02 22:24:56.810 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
05-02 22:24:56.810 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9783: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
05-02 22:24:56.810 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
05-02 22:24:56.900 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
05-02 22:24:56.910 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12215: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
05-02 22:24:56.910 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
05-02 22:24:56.930 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-02 22:24:56.930 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-02 22:24:56.930 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.930 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-02 22:24:56.930 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
05-02 22:24:56.930 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.930 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
05-02 22:24:56.930 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 359: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-02 22:24:56.930 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.940 11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
05-02 22:24:56.940 11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 361: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-02 22:24:56.940 11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:57.170 11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
05-02 22:24:57.190 11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-02 22:24:57.190 11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
05-02 22:24:57.200 11058-11058/com.asd.testapp I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)
这是我第一次创建一个创建缓存的应用程序,所以我不完全理解它是如何工作的。任何帮助表示赞赏。
答案 0 :(得分:0)
我没有看到onCreate里面variable
发生变化的地方。所以你存储值add,它被初始化 - 这就是你从偏好中得到0的原因。
答案 1 :(得分:0)
试试这个 SharedPreferences myPreference = getSharedPreferences(“sharedpref_name”,MODE_PRIVATE); //将数据写入SharedPreferences 编辑器编辑器= myPreference.edit(); editor.putString(“var_name”,“variable_value”); editor.commit();
答案 2 :(得分:0)
SharedPreferences myPreference =getSharedPreferences("sharedpref_name", MODE_PRIVATE);
// Writing data to SharedPreferences
Editor editor = myPreference.edit();
editor.putString("var_name", "variable_value");
editor.commit();