这让我发疯了。似乎无法让这个工作。我很缺乏经验,所以任何提示都会很棒。我希望有人可以解释这只是一个愚蠢的错误。
一个数字来自一个类。然后在该类中使用该数字而没有问题。在该类中,使用共享首选项方法存储该数字。但是在稍后的类中,当我尝试检索该数字时,应用程序崩溃了。
这是我处理并尝试存储数字的类:
public class Summary extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summary);
int Time = gotTime();
// Create object of SharedPreferences.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Summary.this);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putInt("TargetTime", Time);
//commits your edits
editor.
// I then display the Time on that screen which all works ok
TextView textViewT = (TextView) findViewById(R.id.textView4);
textViewT.setText("Time is " + Time);
};
public int gotTime(){
//do stuff to generate the value for time stored as variable PenileTim
return PenileTim;
};
}
然后在以后的课程中,我尝试检索时间:
public class Map extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int VarTime = sharedPref.getInt("TargetTime", (Integer) null);
TextView textViewP = (TextView) findViewById(R.id.textTest);
textViewP.setText(" " + VarTime );
}
}
任何人都知道出了什么问题?