我創建了一個應用程序,按鈕單擊將其整數值從0增加1(它顯示在一個textView中) 我想要做的就是保存該int變量所以重新啓動應用程序後修改的值將保持不變。 我試圖使用這些方法,把它們內部的onClick(增加)的方法,以及在的onCreate,但似乎沒有任何工作:使用SharedPreferences保存int變量(不工作)
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);
退出應用後和空空它的價值reseted 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)
這是我第一次當我創建創建緩存,所以我不完全瞭解它是如何工作的應用程序。任何幫助讚賞。