简体   繁体   English

我怎样才能通过 Intent 转移到 nex Activity

[英]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.我正在尝试制作银行应用程序并尝试制作成功活动,当我尝试从 MainActivity.java 移动到 SuccessActivity.java 时,我得到一个空白页。

This is my MainActivity.java:这是我的 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:这是我的 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:这是我的成功活动。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:这是我的 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:这是我的 Android 清单:

<?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.首先,我不知道您为什么要使用此方法来扩展布局,而不仅仅是 setContentview 方法并将活动作为上下文添加到相应的 xml 中。

second try keeping the call to super.onCreate the first line in your onCreate method第二次尝试保持对 super.onCreate 的调用 onCreate 方法中的第一行

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

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio:如何在我的第二个活动中看到intent方法携带的信息? - Android Studio: how can I see the information carried through the intent method on my second activity? 如何在 Intent intent = new Intent(mContext, c_Report_Activity.class); 处传递 putExtra; - How can I pass putExtra at Intent intent = new Intent(mContext, c_Report_Activity.class); 不能使用意图将数据移动到另一个活动 - Can´t use intent to move data to another activity 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 我如何通过使用Intent从Android中的另一个常规活动中调用特定的选项卡活动 - How can i call a specific tab activity from another regular activity in android by using intent 如何将活动移动到另一个活动? - How do I move activity to another activity? 当我打算获取 arrayList 的数据时,如何在第二个活动和 setText 和 Image 中获取这些数据? - When I intent the arrayList's data, and how can I get those data in the second activity and setText and Image? 如何浏览MySQL更新查询中的所有记录? - How can I move through all the records in MySQL update query? 我可以针对不同的应用程序活动针对隐式意图设置横向方向吗? - Can I set landscape orientation on implicit intent for a diferent app activity? 如何在android中进行新活动? - How do I move to a new activity in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM