Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 使用SharedReferences保存变量,但单击图像后返回1_Java_Android_Sharedpreferences - Fatal编程技术网

Java 使用SharedReferences保存变量,但单击图像后返回1

Java 使用SharedReferences保存变量,但单击图像后返回1,java,android,sharedpreferences,Java,Android,Sharedpreferences,当我退出并重新加载游戏时,我需要保存2个变量,但对我的其他变量有效的SharedReferences不起作用 相关代码: float scoinCount; long scoinLong; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefs = getSharedPreferences("LeagueClicker

当我退出并重新加载游戏时,我需要保存2个变量,但对我的其他变量有效的SharedReferences不起作用

相关代码:

float scoinCount;
long scoinLong;

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     prefs    = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);

     float scoinCount = prefs.getFloat("scoinCount", 1.0f);
     float scoinLong = prefs.getFloat("scoinLong", 0.0f);

    // Added code is below

     this.scoinCount = scoinCount;
     this.scoinLong = (long) scoinLong;

    // Added code is above

     scoinCountMethod();
}

public void scoinCountMethod() {
    ImageView      progressbot    = new ImageView(this);
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

    Resources r = getResources();
    FrameLayout.LayoutParams paramsP = new FrameLayout.LayoutParams((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, displayMetrics.widthPixels, r.getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 23, r.getDisplayMetrics()));

    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

    prefs.edit().putFloat("scoinCount", scoinCount).commit();
    prefs.edit().putFloat("scoinLong", scoinLong).commit();

    if (scoinLong == 400) {

        scoinLong = 0;

        scoinThread.postDelayed(scoinRunnable, 1000);

        scoinCount = 1;

    }

    paramsP.leftMargin = (int) (-dpWidth * scoinCount);

    progressbot = (ImageView) findViewById(R.id.progressbottomid);

    progressbot.setLayoutParams(paramsP);

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.minioncentreid:

    // I am fairly certain the error is somewhere in this onClick method
    scoinCount -= 0.0025;
    scoinLong++;

    if (scoinLong >= 400) {
            scoinThread.postDelayed(scoinRunnable, 1000);
            Log.d("I should run the runnable.",
                    "Code above to run the runnable");
        } else {
            scoinCountMethod();
        }

}

};

@Override
public void onPause() {
    super.onPause();
    prefs.edit().putFloat("scoinCount", scoinCount).commit();
    prefs.edit().putFloat("scoinLong", scoinLong).commit();
}

@Override
public void onResume() {
    super.onResume();
    scoinCount = (long) prefs.getFloat("scoinCount ", 1.0f);
    scoinLong = (long) prefs.getFloat("scoinLong", 0.0f);
}

@Override
public void onStop() {
    super.onStop();
    prefs.edit().putFloat("scoinCount", scoinCount).commit();
    prefs.edit().putFloat("scoinLong", scoinLong).commit();
}
有人知道怎么解决这个问题吗?谢谢你的帮助


编辑:通过添加标记为“added code”的代码,并稍微更改getFloat,现在保存变量,并在启动时加载。但是,单击MinionCenter ID时,scoinCount值似乎返回到1/0.9975

onResume中使用的键名中有一个额外的空格。您使用的是“scoinCount”而不是“scoinCount”。 在简历上修改为

@Override
public void onResume() {
    super.onResume();
    scoinCount = (long) prefs.getFloat("scoinCount", 1.0f);
    scoinLong = (long) prefs.getFloat("scoinLong", 0.0f);
}

尝试将keyname存储在字符串变量中,并在任何地方使用它。这将导致类似这样的错误。

将其放入
prefs=getSharedReferences(“LeagueClicker”,Context.MODE\u PRIVATE)onResume()
onPause()
中,code>刚刚尝试了这个,然后还尝试将代码放在onStop()中,但没有效果。我在上面的代码中省略了其他变量,它们保存得很好,所以我认为这不是问题所在。它一定在代码中的某个地方,但4个小时后,我仍然没有找到任何东西。我做了更改,但它只是让事情变得更糟。总之,变量仍在保存,但单击后,从1/0继续,我不明白为什么。您的意思是从1/0继续单击图像后,scoinCount似乎返回到1,scoinLong似乎返回到0。我之所以说“似乎”,是因为图像的边距重置为-dpwidth。但是dpwidth的值与scoinCount或scoinLong没有关联,那么它们是如何关联的呢