Как передать char1 из метода onCreate в другой метод - в этом случае передать его в 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);
char1 & char2 является глобальным уже, что означает, что они доступны для ссылок целей в любой части вашего класс, за исключением статических методов –
сделать их общедоступными для простоты –
@MocialovBoris Технически переменные 'char1' и' char2' являются * членами *. Java не имеет истинных глобальных переменных. Тем не менее, ваш основной вопрос по-прежнему верен: они доступны как в методах 'onCreate()', так и 'selectChar2()'. –