从另一个 activity、Android 工作室导入数据
Importing data from another activity, Android studio
这是我的主class。我从用户输入中收到双打。
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
中获取这些数据。除此之外,您在 class 的顶部声明了 TextView
,因此它将是 null 在 setContentView(R.layout.activity_pay);
之后在 onCreate(...)
中定义它.在第二个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);
}
这是我的主class。我从用户输入中收到双打。
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
中获取这些数据。除此之外,您在 class 的顶部声明了 TextView
,因此它将是 null 在 setContentView(R.layout.activity_pay);
之后在 onCreate(...)
中定义它.在第二个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);
}