我是一个Android工作室noob,正在为我的学习做评估任务。在我的Android应用程序中,我遇到了2个不是因为错误而出现的问题,我无法确定它们为什么不按预期工作。他们是Toast不会在我的应用程序上显示消息,并且我的Intent不会传递给我的下一个类,它将显示有关输入数据的数据。我没有时间来测试第二节课,因为意图不起作用,任何帮助都会受到赞赏。
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();
}
}
这是我的其他类CalculateActivity(第二个活动)为此布局添加数据到editTexts。
@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);
}
}
下面是Button对象的XML代码(activity_main)。
<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>
答案 0 :(得分:0)
将每个活动中的所有findViewByIds移动到各自的onCreate()方法。您的观点不会在您的buttonclick方法中被引用。
答案 1 :(得分:0)
从您的代码中删除此代码(试试这个,我不知道它是否有效,只是试一试)
//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();
}
在字符串实例化后添加这些行
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();
}