Я - нуб из студии Android, в настоящее время работаю над заданием по оценке для моих исследований. В моем приложении для Android я столкнулся с 2 проблемами, которые не возникают как ошибки, и я не могу точно определить, почему они не работают должным образом. Они заключаются в том, что тост не будет отображать сообщения в моем приложении, и что мое намерение не будет перенесено в следующий класс, который будет отображать данные о введенных данных. У меня действительно не было времени, чтобы протестировать второй класс, потому что Намерение не сработает, любая помощь будет признательна.
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();
}
}
А вот мой класс Other. CalculateActivity (второе действие) добавляет данные для редактирования текстов для этого макета.
@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);
}
}
Ниже приведен мой XML-код (activity_main) для объекта Button.
<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" />
и манифест:
<?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>