【问题标题】:I'm trying to get variable from onCreate() and use in other class我正在尝试从 onCreate() 获取变量并在其他类中使用
【发布时间】:2017-08-16 14:11:37
【问题描述】:
public TextView textView;
public TextView textView1;

//Bank bank = new Bank();

// public int myNumber1;
//public int myNumbe


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    textView = (TextView) findViewById(R.id.textView17);
    textView1 = (TextView) findViewById(R.id.textView18);

    int element = Integer.parseInt(textView.getText().toString());
    final int element1 = element * 3;

    textView1.setText("" + element1);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, Bank.class);
            startActivity(intent);
        }

    });
}

【问题讨论】:

  • 哪个变量?还有哪个班?多一点上下文会有所帮助
  • 我认为您的问题不恰当,请添加一些带有代码的文本以便更好地理解。

标签: java android xml


【解决方案1】:

首先 - 正如其他人在 cmets 中所说,请提供更多信息,以便其他人可以帮助您。 看起来这些是您尝试与其他类共享的控件/变量

public TextView textView;
public TextView textView1;
int element;

您可以通过

将这些传递给另一个类
  • 构造函数参数
  • 方法
  • 公共领域(我个人不喜欢这个)

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2021-04-10
    • 1970-01-01
    • 2020-03-26
    • 2013-04-06
    • 2014-08-30
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多