Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中的共享首选项不';切换活动或关闭应用程序时无法保存_Java_Android_Android Sharedpreferences - Fatal编程技术网

Java Android中的共享首选项不';切换活动或关闭应用程序时无法保存

Java Android中的共享首选项不';切换活动或关闭应用程序时无法保存,java,android,android-sharedpreferences,Java,Android,Android Sharedpreferences,我检查了类似的问题,但似乎没有任何效果。我想不出是什么问题。每次应用程序重新启动或活动切换后,该值变为0 //just parts of code from activity1 SharedPreferences pref; SharedPreferences.Editor editor; // On create.... pref = PreferenceManager.getDefaultSharedPreference

我检查了类似的问题,但似乎没有任何效果。我想不出是什么问题。每次应用程序重新启动或活动切换后,该值变为0

//just parts of code from activity1
            SharedPreferences pref;
            SharedPreferences.Editor editor;
// On create....
            pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            editor = pref.edit();
            max=pref.getInt("mxtime", 0);

//If something>something...
            editor.putInt("mxtime", max);
            editor.commit();
在第一部分中,我在main活动中声明SharedReferences。我将其保存在“max”int中,并且在启动时始终为0,因为如果空值为0。在第二个活动中,我有一个按钮,单击该按钮应清空SharedReferences中的值

活动2:

public class settings extends AppCompatActivity {
private Button myButton;
private Button myButton2;
private Button myButton3;
//sharedPrefs
SharedPreferences pref;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = pref.edit();
    myButton = (Button) findViewById(R.id.button3);
    myButton2 = (Button) findViewById(R.id.button4);
    myButton3 = (Button) findViewById(R.id.button5);

    myButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);


        }
    });
    myButton2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            //sharedPrefs
            editor.remove("mxtime");
            editor.commit();


        }
    });
}
SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
int DEFAULT_FALLBACK_VALUE = 0;  //When value is not received, show this

int VALUE_PASSED = sp.getInt("VARIABLE_KEY", DEFAULT_FALLBACK_VALUE);

// On button click:

int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);
editor.commit();

}

尝试使用以下SharedReference:

 SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE);
 SharedPreferences.Editor editor = preferences.edit();
活动1:

SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);

//If something > something..

int VALUE_TO_PASS = <your value here>;
editor.putInt("VARIABLE_KEY", VALUE_TO_PASS);

// Before screen shift

editor.commit();

为什么要从首选项
编辑器中删除首选项。删除(“mxtime”)?您在哪里设置max的值呢?在将max保存到SharedPrefs之前,我设置了max的值。我将editor.remove改为editor.clear()。为什么要从首选项
editor.clear()中删除所有首选项?您分配给
max
的值是哪个?因为该按钮用于重置首选项中的所有值。但是,即使我没有转到第二个活动并单击它,也不会保存值。感谢您的帮助,现在发现了问题所在。这在我尝试它时起作用,然后我第二次尝试它,它再次变为0。我搞不懂发生了什么事wrong@BuiQuangHuy
editor.apply()。