首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用SharedPreferences保存int变量(不起作用)

使用SharedPreferences保存int变量(不起作用)
EN

Stack Overflow用户
提问于 2015-05-03 14:44:05
回答 4查看 551关注 0票数 0

我创建了一个应用程序,点击一下按钮,它的整数值就会从0增加1。(它显示在一个textView中)我要做的是保存这个int变量,这样在重启应用程序后,修改后的值将保持不变。我试图使用这些方法,在onCreate中也将它们放在onClick(increase)方法中,但似乎都不起作用:

代码语言:javascript
运行
复制
SharedPreferences mPrefs = getSharedPreferences("label", 0);
Integer var = mPrefs.getInt("var", variable);

SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putInt("tag", variable).commit();

还有这个:

代码语言:javascript
运行
复制
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
SharedPreferences.Editor editor= sharedPref.edit();
editor.putInt("name", variable);
editor.commit();

getSharedPreferences("name", variable);

退出并加载应用程序后,该值将重置为0

下面是完整的代码:

代码语言:javascript
运行
复制
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所说的(可能不重要..):

代码语言:javascript
运行
复制
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)

这是我第一次创建一个创建缓存的应用程序,所以我不完全理解它是如何工作的。感谢您的帮助。

EN

回答 4

Stack Overflow用户

发布于 2015-05-03 14:57:45

在您的TestApp活动中,var总是返回0。下面是存储在MyPrefsFile文件key=silentMode中的所有值

如何从SharedPreferences获取值

代码语言:javascript
运行
复制
       public static final String PREFS_NAME = "MyPrefsFile";
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //PREFS_NAME is file name not your key name
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);

如何存储值

代码语言:javascript
运行
复制
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();

你真的需要访问Official Doc - Storage Options Shared Preferences

票数 0
EN

Stack Overflow用户

发布于 2015-05-03 15:09:10

我在onCreate内部看不到variable发生变化的任何地方。所以你存储了初始化后的值add - 0。这就是为什么你总是从首选项得到0的原因。

票数 0
EN

Stack Overflow用户

发布于 2015-05-03 16:52:09

试试这个编辑器SharedPreferences myPreference =getSharedPreferences("sharedpref_name",MODE_PRIVATE);//将数据写入编辑器编辑器= myPreference.edit();editor.putString("var_name","variable_value");editor.commit();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30011027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档