简体   繁体   中英

How can I move to nex Activity through Intent

I am trying to make a bank app and I'm trying to make a Success Activity, when I try to move from MainActivity.java to SuccessActivity.java I get a blank page.

This is my MainActivity.java:

package com.example.bankapp.ui;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bankapp.R;
import com.example.bankapp.data.AccountRepository;
import com.example.bankapp.databinding.ActivityMainBinding;
import com.example.bankapp.domain.BankAccount;
import com.example.bankapp.domain.SuccessPayment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private BankAccount bankAccount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bankAccount = new AccountRepository().getAccount();
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        bindBankDataToRadioButtons(bankAccount);
        bindBankDataToViews(bankAccount);
        binding.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                binding.bnpagar.setEnabled(true);
                if (checkedId == R.id.rbotraCantidad) {
                    binding.etnuevaCantidad.setVisibility(View.VISIBLE);
                    binding.ingreseCantidad.setVisibility(View.VISIBLE);
                } else {
                    binding.etnuevaCantidad.setVisibility(View.GONE);
                    binding.ingreseCantidad.setVisibility(View.GONE);
                }
            }
        });

        binding.bnpagar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double amountToPay = getAmountToPay();
                if (amountToPay == -1.0) {
                    showToastMessage(getString(R.string.err_cantidadIncorrecta));
                } else if (amountToPay <= bankAccount.getAccountBalance()) {
                    SuccessPayment successPayment = makePayment(amountToPay, bankAccount);
                    Intent intent = new Intent(getApplicationContext(), SuccessActivity.class);
                    intent.putExtra("SUCCESS_EXTRA", successPayment);
                    startActivity(intent);
                    finish();

                } else {
                    showToastMessage(getString(R.string.err_saldoInsuficiente));
                }
            }
        });
    }




    private double getAmountToPay() {
        double amount;
        switch (binding.rbGroup.getCheckedRadioButtonId()) {
            case R.id.rbdeudaActual:
                amount = bankAccount.getCurrentDebt();
                break;
            case R.id.rbsinInteres:
                amount = bankAccount.getMinimumPaymentDue();
                break;
            case R.id.rbpagoMinimo:
                amount = bankAccount.getPaymentDue();
                break;
            default:
                amount = getAmountFromEditText(binding.etnuevaCantidad);
        }
        return amount;
    }

    private double getAmountFromEditText(EditText editText) {
        try {
            String text = editText.getText().toString();
            return Double.parseDouble(text);
        } catch (NumberFormatException e) {
            return -1.0;
        }
    }

    private void bindBankDataToRadioButtons(BankAccount bankAccount) {
        binding.rbdeudaActual.setText(
                getString(R.string.label_deudaActual, bankAccount.getCurrentDebt())
        );
        binding.rbsinInteres.setText(
                getString(R.string.label_sinInteres, bankAccount.getMinimumPaymentDue())
        );
        binding.rbpagoMinimo.setText(
                getString(R.string.label_pagoMinimo, bankAccount.getPaymentDue())
        );
    }

    private void bindBankDataToViews(BankAccount bankAccount) {
        binding.cardLayout.tvAccountName.setText(bankAccount.getAccountName());
        //El string label_peso_sign pide un número, por eso el método getString recibe
        //un segundo parámetro, que en este caso es el accountBalance.
        binding.cardLayout.tvAccountBalance.setText(getString(R.string.label_peso_sign, bankAccount.getAccountBalance()));
        binding.cardLayout.tvAccountNumber.setText(bankAccount.getAccountNumber());
        binding.cardLayout.tvCardName.setText(bankAccount.getCardName());
        binding.cardLayout.tvCardNumber.setText(bankAccount.getCardNumber());
        binding.cardLayout.tvCurrentDebtAmount.setText(getString(R.string.label_peso_sign, bankAccount.getCurrentDebt()));
        binding.cardLayout.tvClosingDateBalanceAmount.setText(getString(R.string.label_peso_sign, bankAccount.getClosingDateBalance()));
        binding.cardLayout.tvPaymentDate.setText(bankAccount.getPaymentDueDate());
    }


    private void showToastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    private SuccessPayment makePayment(double amount, BankAccount bankAccount) {
        String date = new SimpleDateFormat(
                "dd 'de' MMMM 'de' yyyy",
                new Locale("es", "MX")
        ).format(new Date(Calendar.getInstance().getTimeInMillis()));
        String referenceNumber = UUID.randomUUID().toString().substring(0, 10);
        // amount -> double
        // date -> String
        // referenceNumber -> String
        // bankAccount -> BankAccount
        return new SuccessPayment(
                amount,
                date,
                referenceNumber,
                bankAccount
        );
    }


}

This is my Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/cardLayout"
        layout="@layout/card_layout" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rbGroup">

        <RadioButton
            android:id="@+id/rbdeudaActual"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:text="@string/label_deudaActual" />

        <RadioButton
            android:id="@+id/rbpagoMinimo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:text="@string/label_pagoMinimo" />

        <RadioButton
            android:id="@+id/rbsinInteres"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/label_sinInteres" />

        <RadioButton
            android:id="@+id/rbotraCantidad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/label_otraCantidad" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginStart="5dp"
            android:text="@string/label_nuevaCantidad"
            android:id="@+id/ingreseCantidad"/>

        <EditText
            android:id="@+id/etnuevaCantidad"
            android:layout_marginStart="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:imeOptions="actionDone">
        </EditText>

    </RadioGroup>

    <Space
        android:id="@+id/espacio"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <Button
        android:id="@+id/bnpagar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/label_pagar" />

</LinearLayout>

This is my Success Activity.java:

package com.example.bankapp.ui;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bankapp.R;
import com.example.bankapp.databinding.ActivitySuccessBinding;
import com.example.bankapp.domain.SuccessPayment;


public class SuccessActivity extends AppCompatActivity {

    private ActivitySuccessBinding binding;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        binding = ActivitySuccessBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_success);
        SuccessPayment payment = getIntent().getParcelableExtra("SUCCESS_EXTRA");
    }
}

This is my ActivitySuccess.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/label_current_debt"
        tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>

This is my Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bankapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BankApp">

        <activity android:name=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ui.SuccessActivity" > </activity>




</application>

</manifest>

first i don't know why you are using this method to inflate the layout instead of just the setContentview method and adding the activity as context in the corresponding xml.

second try keeping the call to super.onCreate the first line in your onCreate method

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        

    binding = ActivitySuccessBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    setContentView(R.layout.activity_success);
    SuccessPayment payment = getIntent().getParcelableExtra("SUCCESS_EXTRA");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM