gpt4 book ai didi

java - 共享首选项并发症

转载 作者:行者123 更新时间:2023-12-02 08:13:37 24 4
gpt4 key购买 nike

嘿,所以我尝试使用共享首选项来存储在一个 Activity 上提交的信息,然后在另一个 Activity 上调用并显示该信息。我认为我的理解是正确的,但它在键下的 View 类上给出了红色下划线。

public class AddGame extends Activity{
private static final String[] earnedRuns = {
"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"};
private static final String[] inningsP = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"};
private static final String[] inningsP2 = {".00",".33", ".66"};
private static final String[] strikeOuts = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"};


double ip=0;
int k=0;

public static final String PREFS_NAME = "GameSaved";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addgame);


final DecimalFormat formatter = new DecimalFormat("0.000");

//Pitcher Stats

final Spinner spin1 = (Spinner) findViewById(R.id.spin1);
ArrayAdapter<String> spinn1 = new
ArrayAdapter<String>(this, R.layout.spinner_entry, inningsP);
spinn1.setDropDownViewResource(R.layout.spinner_entry);
spin1.setAdapter(spinn1);

final Spinner spin2 = (Spinner) findViewById(R.id.spin2);
ArrayAdapter<String> spinn2 = new
ArrayAdapter<String>(this, R.layout.spinner_entry, inningsP2);
spinn2.setDropDownViewResource(R.layout.spinner_entry);
spin2.setAdapter(spinn2);

final Spinner spin3 = (Spinner) findViewById(R.id.spin3);
ArrayAdapter<String> spinn3 = new
ArrayAdapter<String>(this, R.layout.spinner_entry, strikeOuts);
spinn3.setDropDownViewResource(R.layout.spinner_entry);
spin3.setAdapter(spinn3);

Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener()
{
public void onClick(View calculate)
{



ip = Integer.parseInt((String) spin1.getSelectedItem()) + Double.parseDouble((String) spin2.getSelectedItem());
k = Integer.parseInt((String) spin3.getSelectedItem());


SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
saveGame(editor);
editor.commit();

Intent i = new Intent(AddGame.this, CurSeason.class);
startActivity(i);
}
});
}

public void saveGame(SharedPreferences.Editor map){
if (map == null) {
return;
}

//Pitching
map.putString("INNINGS_PITCHED", Double.toString(ip));
map.putInt("STRIKEOUTS", k);



}

}

这是应该调用它的 View 。

public class ViewCurrent extends Activity{
public static final String PREFS_NAME = "GameSaved";
int k=0;
double ip=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcurrent);


SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
k=saved.getInt(STRIKEOUTS, 0);
ip=saved.getString(INNINGS_PITCHED, null);


}

}

如有任何帮助,我们将不胜感激。提前致谢

最佳答案

在 View 类中将键用双引号引起来,例如

k=saved.getInt("三振", 0);ip=saved.getString("INNINGS_PITCHED", null);

您也可以使用“”代替 null。

关于java - 共享首选项并发症,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com