2011-07-29 74 views
0

嘿所以我試圖使用共享首選項來存儲在一項活動中提交的信息,然後將它重新調用並顯示在另一項活動上。我認爲我有它的正確性,但它在關鍵下的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); 


} 

}

任何幫助,將不勝感激。在此先感謝

回答

1

在視圖類包圍像

K = saved.getInt在雙引號鍵( 「三振」,0); ip = saved.getString(「INNINGS_PITCHED」,null);

你也可以用「」替代null。

+0

謝謝你們。我無法相信我沒有注意到我沒有在他們周圍「」。很好的工作注意到這一點。 – steven

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

這應該解決它。