我有一个应用程序有一个名为Money的int和我.putExtra它去另一个类,但每次我按下按钮去那个类它强制关闭应用程序。
这是我用来发送int的代码
int Money = 0;
GoToSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), settings.class);
intent.putExtra("money",Money);
startActivity(intent);
}
});
这是我收到它并尝试保存的地方
public class settings extends Activity {
int LoadDef = 0;
Button Save,Load,ClsSave;
Intent intent = getIntent();
int Money = intent.getIntExtra("money", 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Save = (Button) findViewById(R.id.Save);
Load = (Button) findViewById(R.id.Load);
ClsSave = (Button) findViewById(R.id.ClsSave);
Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences=getSharedPreferences("MoneySave", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("MoneySave", Money);
editor.commit();
}
});
有人可以帮我理清它为什么会强行关闭吗?
答案 0 :(得分:2)
问题在于:
Intent intent = getIntent();
你应该从onCreate()
而不是课程定义中获得额外的意图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Intent intent = getIntent();
int Money = intent.getIntExtra("money", 0);