共享首选项并发症

时间:2011-07-29 00:59:17

标签: java android sharedpreferences

嘿所以我正在尝试使用“共享首选项”来存储在一个活动上提交的信息,然后将其调用并显示在另一个活动上。我以为我把它弄错了,但它在键下的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);


}

}

任何帮助将不胜感激。提前致谢

2 个答案:

答案 0 :(得分:1)

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

那应该解决它。

答案 1 :(得分:1)

在视图类中,将键用双引号括起来,如

k = saved.getInt(“STRIKEOUTS”,0); ip = saved.getString(“INNINGS_PITCHED”,null);

你也可以用“”代替null。