基本上,我一直在尝试将对象数据传递给另一个活动的项目。 我要做什么:在我的主活动中,我单击一个按钮,该按钮将我重定向到另一个活动,并通过意图传递数据,然后我可以在该活动中查看。另外,由于它是一个对象,因此当我在此活动中对其进行更改时,我希望它也在主活动中也进行更改。我的问题是,当我第一次启动应用程序时,对象数据已传递并且可以在Toast中看到它。但是,一旦将其放入“文本视图”中,它就会崩溃并且根本不会传递任何数据。最糟糕的是,Android Studio不会给我任何错误。
我尝试用不同的方法甚至使用捆绑包来创建Intent。什么都没做。另外,我提到Android Studio不会给我任何错误,因此,我什至不知道是什么使它特别崩溃了。任何帮助将不胜感激。
这是我的帐户类别(对象):
public class Account {
private int accountNumber;
private double balance;
private String bankName;
public Account(int accountNumber, double balance, String bankName) {
this.accountNumber = accountNumber;
this.balance = balance;
this.bankName = bankName;
}
public int getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public String getBankName() {
return bankName;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
这是我用来传递对象的主要活动(btnActivity2调用与该问题无关的活动):
public class MainActivity extends AppCompatActivity {
Account account;
private int accountNumber;
private double balance;
private String bankName;
private Intent intent;
private Button btnActivity2;
private Button btnActivity3;
private EditText etDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnActivity2 = findViewById(R.id.btnActivity2);
btnActivity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}
});
try {
account = new Account(2078910, 100000, "Chase");
accountNumber = account.getAccountNumber();
balance = account.getBalance();
bankName = account.getBankName();
etDisplay = findViewById(R.id.etDisplay);
etDisplay.setText("Account Number: " + accountNumber + "\nBalance: " + balance + "\nBank Name: " + bankName + "\n");
} catch (NullPointerException npe) {
Toast.makeText(getApplicationContext(), npe.getMessage(), Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException iae) {
Toast.makeText(getApplicationContext(), iae.getMessage(), Toast.LENGTH_LONG).show();
}
btnActivity3 = findViewById(R.id.btnActivity3);
btnActivity3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Activity3.class);
intent.putExtra("accountNumber", accountNumber);
intent.putExtra("balance", balance);
intent.putExtra("bankName", bankName);
startActivity(intent);
}
});
}
}
这是Activity3,这是我要传递给的活动:
public class Activity3 extends AppCompatActivity {
private Intent intent;
private EditText etAmount;
private TextView tvInfo;
private Button withdraw, deposit;
private double amount;
private int accountNumber;
private double balance;
private String bankName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
etAmount = findViewById(R.id.etAmount);
tvInfo = findViewById(R.id.etAccountInfo);
amount = Double.parseDouble(etAmount.getText().toString());
try {
intent = getIntent();
accountNumber = intent.getIntExtra("accountNumber", 100000);
balance = intent.getDoubleExtra("balance", 1000);
bankName = intent.getStringExtra("bankName");
Toast.makeText(getApplicationContext(), accountNumber + "\n" + balance + "\n" + bankName, Toast.LENGTH_LONG).show();
} catch (NullPointerException npe) {
Toast.makeText(getApplicationContext(), npe.getMessage(), Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException iae) {
Toast.makeText(getApplicationContext(), iae.getMessage(), Toast.LENGTH_LONG).show();
}
// tvInfo.setText("Account Number: " + accountNumber + "\nBalance: " + balance + "\nBank Name: " + bankName + "\n");
withdraw = findViewById(R.id.btnWithdraw);
withdraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
deposit = findViewById(R.id.btnDeposit);
deposit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
MainActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="412dp"
android:layout_height="195dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/complimentary" />
<Button
android:id="@+id/btnActivity2"
android:layout_width="378dp"
android:layout_height="70dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:text="Second Activity"
app:layout_constraintBottom_toTopOf="@+id/btnActivity3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnActivity3"
android:layout_width="378dp"
android:layout_height="70dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Third Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/etDisplay"
android:layout_width="378dp"
android:layout_height="330dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintBottom_toTopOf="@+id/btnActivity2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</android.support.constraint.ConstraintLayout>
Activity3 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity3">
<EditText
android:id="@+id/etAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Amount $:"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etAccountInfo" />
<Button
android:id="@+id/btnDeposit"
android:layout_width="210dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Deposit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnWithdraw"
app:layout_constraintVertical_bias="0.083" />
<Button
android:id="@+id/btnWithdraw"
android:layout_width="210dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
android:text="Withdraw"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etAmount" />
<EditText
android:id="@+id/etAccountInfo"
android:layout_width="258dp"
android:layout_height="186dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
当我按btnActivity3时,应用程序崩溃:有时显示应用程序关闭消息,有时甚至根本无法工作。正如我之前所说,我没有收到任何错误消息,所以我什至不知道它有什么问题。请帮忙!
答案 0 :(得分:1)
问题出在以下几行:
amount = Double.parseDouble(etAmount.getText().toString());
您未在XML中设置etAmount
的内容,因此在尝试分析空行时会抛出NumberFormatException
。