我尝试保存int" redRing"的值。来自班级" Ring"使用SharedPreferences显示在另一个活动的TextView中,它可以工作但不是每次都有效,如果我退出应用程序并在我的应用程序中再次返回但有时不是每次都重置我的int值。如果有人可以试着告诉我代码的错误,我做了一个简单的小代码示例?
1)班级戒指:
public class Ring { // User search rings with different colors
public static int color;
public static int redRing, greenRing, nbRing, somment, someGreen, someRing;
public static TextView tvNbRing, tvRingColor;
// Constuctor
public Ring(int c) { this.color = c; }
// Getter
public int getColor() { return this.color; }
public void setColor(int dice) { // For the example there’s 2 colors
dice = (int)(Math.random()*2+1);
this.color = dice;
if(this.color == 1) { // If 1 so user find a red ring
redRing = someint + 1;
tvRingColor.setText("It's a red ring");
}
if(this.color == 2) {
greenRing = someGreen + 1; // I add the green color
tvRingColor.setText("It's a green ring");
}
}
public int setNbRingFind() { // Nb = Number
int rings = (int)(Math.random()*2+0); // User can find 0 or 1 ring
Ring r = new Ring(this.color);
if(rings>0) {
nbRing = someRing + 1;
tvNbRing.setText("You find " + rings + " ring");
r.setColor(this.color);
} else { tvNbRing.setText("Nothing find, try again"); }
return rings;
}
}
2)活动1:主要
import static fr.ringshared.Ring.tvNbRing;
import static fr.ringshared.Ring.tvRingColor;
import static fr.ringshared.Ring.redRing;
import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.greenRing;
import static fr.save.sharedpreferences.Ring.nbRing;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;
public class MainActivity extends Activity {
Button search, stock, useRing;
TextView tvRing;
int x = 0;
Ring r = new Ring(x); // Create a Ring with a x color
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (Button) findViewById(R.id.radar);
stock = (Button) findViewById(R.id.capsules);
useRing = (Button) findViewById(R.id.useRing);
tvRing = (TextView) findViewById(R.id.ring);
tvNbRing = (TextView) findViewById(R.id.tvNbRing);
tvRingColor = (TextView) findViewById(R.id.tvRingColor);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
tvRing.setText("Objets trouvés : " + String.valueOf(someRing));
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
addRing();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("someint", redRing);
editor.putInt("someGreen", greenRing);
editor.putInt("someRing", bring);
editor.apply();
editor.commit();
lastNumber();
}
});
stock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentStock = new Intent(MainActivity.this, StockActivity.class);
startActivity(intentStock);
}
});
}
public void addRing() { // I use a method from the class Ring
r.setNbRingFind();
}
public void lastNumber(){
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
tvRing.setText("Objects find : " + String.valueOf(someint));
}
}
3)活动2:StockActivity
import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;
public class StockActivity extends Activity {
TextView tvRing2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capsules);
tvRing2 = (TextView) findViewById(R.id.ring2);
tvGreen = (TextView) findViewById(R.id.green);
tvTotal = (TextView) findViewById(R.id.total);
tvRing2.setText("Red rings find : " + String.valueOf(someint));
tvGreen.setText("Green rings find " + String.valueOf(someGreen));
tvTotal.setText("All rings find : " + String.valueOf(someRing));
}
}
如果有人可以帮我找到我的代码有问题的地方,因为在逻辑上没有错误显示,只是在运行期间,有时候int" redRing"或者"某些"已重置为0而不是保留其最后一个数字。
答案 0 :(得分:1)
getSharedPreferences(String name, int mode)
来电。如果您使用活动中的上下文,则可能找不到SharedPreferences键。您可以尝试getApplicationContext().getSharedPreferences(String name, int mode)