-1

Basically, I've been working on a project where I am trying to pass object data to another activity. What I am trying to do: In my main Activity, I click on a button which redirects me to another activity and also passes data through intent which I can then view in that activity. Also, since it is an object, when I change it in this activity, I want it to also change in the Main Activity. My problem is, when I first start the application, the object data is passed and I can see it in Toast. However, once I put it into a Text View, it crashes and doesn't pass any data at all. The worst part is that Android Studio doesn't give me any errors for it.

I've tried creating Intent different ways and even with a bundle. Nothing has been working. Also, I mentioned that Android Studio doesn't give me any errors, therefore, I don't even know what is making it crash specifically. Any help will be appreciated.

This is my Account Class (Object):

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;
    }
}

This is the Main Activity which I am using to pass the object (btnActivity2 calls an activity that is unrelated to this question):

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);
            }
        });
    }
}

This is Activity3, this is the activity that I am trying to pass to:

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>

When I press on the btnActivity3, the app crashes: sometimes showing app closes message and sometimes just not working at all. As I've said before, I didn't get any error messages, so I don't even know what is wrong with it. Please Help!

Roman Roshchuk
  • 147
  • 1
  • 1
  • 11

1 Answers1

1

The problem is in following line:

 amount = Double.parseDouble(etAmount.getText().toString());

You didn't set etAmount content in the XML, so it throws NumberFormatException when tries parse empty line.

S-Sh
  • 3,564
  • 3
  • 15
  • 19