为了账号安全,请及时绑定邮箱和手机立即绑定

在方法范围外使用变量

在方法范围外使用变量

Smart猫小萌 2019-03-14 14:15:44
需要在onCreate方法之外传递char1到另一个方法selectChar2中。可以设置char1或者char2为全局吗?不知道怎么实现。谢谢private String char1,char2; // does this even do anything? I thought this would let me use char1 and char2 anywhere. @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_pick_char2);     Bundle extras = getIntent().getExtras();     if (extras != null) {         String char1 = (String) extras.getString("char1"); //is the (String) necessary?         TextView textView = (TextView) findViewById(R.id.header);         textView.setText(char1+" vs "+"char2");     } } public void selectChar2(View v) {     Button btn = (Button)v;     String char2 = btn.getText().toString();     Intent intent = new Intent(PickChar2.this, DisplayMatchUp.class);     Bundle extras = new Bundle();         extras.putString("Character1",char1); //char1 here is null...how do I get the value from the method above?     extras.putString("Character2", char2);     intent.putExtras(extras);     startActivity(intent);
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 442 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号