简体   繁体   中英

Toast not displaying in activity_main / Intent not switching activites/ layout

i'm an Android studio noob currently working on an assessment task for my studies. In my Android application I have encountered 2 problems that are not arising as errors, and I cannot pinpoint why they arent working as intended. They are that the Toast won't show messages on my application, and that my Intent will not pass over to my next class which will display data about the data entered. I have not really had the time to test the second class because the Intent won't work, any help would be appreciated.

Main_Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
//onClick method
//check to see whether or not fields are empty
//if empty then toast
public void onButtonClick(View v){

    //instantiate the EditTexts
    loan = findViewById(R.id.LoanEditText);
    interest = findViewById(R.id.InterestEditText);
    years = findViewById(R.id.YearsEditText);

    //instantiate strings for retrieving EditText values
    loanString = loan.getText().toString();
    interestString = interest.getText().toString();
    yearsString = years.getText().toString();

    //check that all fields have a value
    checkIfEmpty(loanString);
    checkIfEmpty(interestString);
    checkIfEmpty(yearsString);

    //check if a radio has been selected in the RadioGroup
    rg = findViewById(R.id.radioGroup);
    checkIfRadioSelected(rg);

    //create intent
    intent = new Intent(this, CalculateActivity.class);
    //add EditText values to the intent
    intent.putExtra("loanValue", loanString);
    intent.putExtra("interestValue", interestString);
    intent.putExtra("yearsValue", yearsString);
    //start the intent
    startActivity(intent);
}

//method that handles whether an EditText is empty
public void checkIfEmpty(String s){
    if(s.matches("")){
       Toast.makeText(this,"Please enter data in all fields to proceed.", Toast.LENGTH_SHORT).show();

    }
}

//method that handles whether a button has been checked
public void checkIfRadioSelected(RadioGroup rg){
    if (rg.getCheckedRadioButtonId() == -1){
        // no radio buttons are checked
        Toast.makeText(this,"Please select a RadioButton to proceed.", Toast.LENGTH_SHORT).show();
    }
}

And here is my Other class CalculateActivity (second activity) adds data to editTexts for this layout.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculate_activity_layout);

    //respond to the calculate method in MainActivity
    //use the bundle to get retrieve the sent intent
    Bundle bundle = getIntent().getExtras();

    //assign the values of the EditTexts to a series of Strings
    //that we can manipulate
    loanValue = bundle.getString("loanValue");
    interestValue = bundle.getString("interestValue");
    yearsValue = bundle.getString("yearsValue");

    //call method to calculate the values, use Strings as inputs
    //convert strings to integers so they can be used properly
    mortgageCalculation(loanValue, interestValue, yearsValue);
}

//calculate the payments, respond appropriately to the correct
//frequency of payment
public void mortgageCalculation(String loan, String interest, String years){

    //Instantiate the RadioButtons to see which has been checked
    weekly = findViewById(R.id.WeeklyRadioButton);
    fortnightly = findViewById(R.id.FortnightlyRadioButton);
    monthly = findViewById(R.id.MonthlyRadioButton);

    //convert the parameter strings into integers to calculate
    loanInt = Double.parseDouble(loan);
    interestInt = Double.parseDouble(interest);
    yearsInt = Double.parseDouble(years);

    //check the button, set Text, calculate the payments,
    //use a for loop to iterate over the frequency of payment,
    //and the years in which it is payed over
    if (weekly.isChecked()){
        FrequencyTextView.setText(R.string.WeeklyRepayments);
        frequencyInt = 52;
        //call upon the method to translate repayment using weekly payments
        //(loan repayment)
        repaymentBalance(loanInt, interestInt, yearsInt, frequencyInt);

        //call upon the method to translate the balance at the end of each year
        //(EditTexts looped over)
        balanceAtEndOfYear(loanInt, interestInt, yearsInt);

    }
    else if (fortnightly.isChecked()) {
        if (monthly.isChecked()){
            FrequencyTextView.setText(R.string.MonthlyRepayments);
            frequencyInt = 12;
            repaymentBalance(loanInt, interestInt, yearsInt, frequencyInt);
            balanceAtEndOfYear(loanInt, interestInt, yearsInt);
        }
    }
    else {
        FrequencyTextView.setText(R.string.FortnightlyRepayments);
        frequencyInt = 26;
        repaymentBalance(loanInt, interestInt, yearsInt, frequencyInt);
        balanceAtEndOfYear(loanInt, interestInt, yearsInt);
    }

}

//repayment calculation method
public void repaymentBalance(double l, double i, double y, double f){
   //change value of i, because i must be divided by i later, does not work
    value = 0;

    value = l * (1 + i / 365);
    value += Math.pow(i, 365 * y) * (1 + i / 365);
    value += Math.pow(f, -1) / (1+ i / 365);
    value += Math.pow(365 * y, -1);

    //frequency payment total
    double FrequencyPaymentTotal;
    FrequencyPaymentTotal = value / y / f;
    FrequencyPaymentEditText = findViewById(R.id.FrequencyPaymentEditText);
    String FrequencyPaymentString = String.valueOf(FrequencyPaymentTotal);
    FrequencyPaymentEditText.setText(FrequencyPaymentString);

    //set values value of EditText
    //have to parse value to a string
    LoanRepaymentEditText = findViewById(R.id.LoanRepaymentEditText);
    String LoanRepaymentString = String.valueOf(value);
    LoanRepaymentEditText.setText(LoanRepaymentString);

    //total interest charge
    totalMinusInterest = value / i;
    interestCharged = value - totalMinusInterest;
    InterestChargeEditText = findViewById(R.id.InterestChargeEditText);
    String InterestChargedString = String.valueOf(interestCharged);
    InterestChargeEditText.setText(InterestChargedString);
}

public void balanceAtEndOfYear(double l, double i, double y){
    double b = y;

    for (int x = 0; x < y; x++){
        b += l * (1+ i / 365);
        b += Math.pow(b, 365 * y) - i * (1 + i / 365);
        b += Math.pow(b, 365 * y - 1) / (1 + i / 365);
        b += Math.pow(b, y -1);

        EditText et = new EditText(this);
        String YearlyBalance = String.valueOf(b);
        et.setText(YearlyBalance);

        //add the EditText to a LinearLayout located in a ScrollView for each year
        ll = findViewById(R.id.LinearLayoutScroll);
        ll.addView(et);
    }
}

Below is my XML code (activity_main) for the Button object.

 <Button
    android:id="@+id/CalculateButton"
    android:layout_width="213dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="onButtonClick"
    android:text="@string/calculate_payments"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    app:layout_constraintVertical_bias="0.44" />

and the manifest:

<?xml version="1.0" encoding="utf-8"?>

<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/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CalculateActivity" />
</application>

Move all your findViewByIds in each activity to their respective onCreate() methods. Your views don't get referenced in your buttonclick method.

Remove this code from your code (Try this, I don't know if it works, just give it a try)

//check that all fields have a value
checkIfEmpty(loanString);
checkIfEmpty(interestString);
checkIfEmpty(yearsString);

//method that handles whether an EditText is empty
public void checkIfEmpty(String s){
if(s.matches("")){
   Toast.makeText(this,"Please enter data in all fields to proceed.",Toast.LENGTH_SHORT).show();
}

And add these lines after your strings instantiation

if(TextUtils.isEmpty(loanString) || TextUtils.isEmpty(interestString) || TextUtils.isEmpty(yearsString)) { 
Toast.makeText(this,"Please enter data in all fields to proceed.", Toast.LENGTH_SHORT).show();
}

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