gpt4 book ai didi

java - 从另一个 Activity Android studio 导入数据

转载 作者:行者123 更新时间:2023-12-01 22:29:53 24 4
gpt4 key购买 nike

这是我的主课。我从用户输入中收到 double 。

public void onPayClick(View view){
Intent payIntent = new Intent(this, payActivity.class);

final EditText amountInput = (EditText) findViewById(R.id.amountInput);
final EditText yearInput = (EditText) findViewById(R.id.yearInput);
final EditText interestInput = (EditText) findViewById(R.id.interestInput);

double amountDouble = Double.parseDouble(amountInput.getText().toString());
double yearDouble = Double.parseDouble(yearInput.getText().toString());
double interestDouble = Double.parseDouble(interestInput.getText().toString());

payIntent.putExtra("amountDouble", amountDouble);
payIntent.putExtra("yearDouble", yearDouble);
payIntent.putExtra("interestDouble", interestDouble);

startActivity(payIntent);
}

这是我的第二个 Activity ,在这里我想计算贷款,但我只是想打印 amountDouble 来看看它是否有效,但它不起作用......

TextView printPay = (TextView) findViewById(R.id.printPay);


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);

Bundle payData = getIntent().getExtras();

if(payData == null){
printPay.setText("Villa, þú verður að filla upp í alla reiti.");
return;
}
String amountDouble = payData.getString("amountDouble");
String yearDouble = payData.getString("yearDouble");
String interestDouble = payData.getString("interestDouble");

String sum1 = amountDouble;
printPay.setText(sum1);
}

每次我单击 payButton 时,应用程序都会崩溃。

最佳答案

从第一个Activity开始,您直接在intent中添加这些值,因此您必须从intent而不是从bundle获取这些数据。除此之外,您在类的顶部声明 TextView ,因此它将 nullonCreate(...) 中定义它 setContentView(R.layout.activity_pay);。在第二个Activity中像这样改变

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
TextView printPay = (TextView) findViewById(R.id.printPay);

String amountDouble = String.valueOf(getIntent().getDoubleExtra("amountDouble", 0.0));
String yearDouble = String.valueOf(getIntent().getDoubleExtra("yearDouble", 0.0);
String interestDouble = String.valueOf(getIntent().getDoubleExtra("interestDouble", 0.0);

String sum1 = amountDouble;
printPay.setText(sum1);
}

more info of getDoubleExtra(java.lang.String, double)

关于java - 从另一个 Activity Android studio 导入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28021916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com