嘿,所以我试图使用“共享首选项”存储在一个活动中提交的信息,然后将其调回并显示在另一个活动中。我以为我没错,但是在键下的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);
}
}
这是应该调用它的视图。
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);
}
}
任何帮助,将不胜感激。提前致谢
最佳答案
在视图类中,将键括在双引号中,例如
k = saved.getInt(“ STRIKEOUTS”,0);
ip = saved.getString(“ INNINGS_PITCHED”,null);
您也可以使用“”代替null。
关于java - 共享首选项并发症,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6867539/