简体   繁体   中英

When I Click the Run Button, Apps Stops running with an error- Android App

I am programming a mortgage calculator app. I thought I have finished the app and it should work now. However every time I click the button "Calculate Monthly Payment", the app stops and exit immediately with the error message "Unfortunately, your app has stopped" .

Here is my code fragment for the MainActivity.java :

public class MainActivity extends ActionBarActivity 
{
    Button button;
    EditText borrowedAmt;
    SeekBar interestRate;
    RadioGroup loanTerm;
    RadioButton termChosen;
    CheckBox taxAndInsur; 
    TextView monthlyPay;

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

        borrowedAmt = (EditText) findViewById(R.id.editText1);
        loanTerm = (RadioGroup) findViewById(R.id.radioGroup1);
        monthlyPay = (TextView) findViewById(R.id.textView4);
        taxAndInsur = (CheckBox) findViewById(R.id.checkBox1);
        interestRate = (SeekBar) findViewById(R.id.seekBar1);
        button = (Button) findViewById(R.id.button1);

        // SeekBar
        interestRate.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
        {
            int progressChanged = 0;

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
            {
                progressChanged = progress;
            }

            public void onStartTrackingTouch(SeekBar seekBar) 
            {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) 
            {
                Toast.makeText(MainActivity.this,"Rate chosen = " + progressChanged, 
                        Toast.LENGTH_SHORT).show();
            }
        });

        //Button

        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0)
            {
                double borrowedAmtValue = 0.0;
                double rateValue = 0.0;
                int noOfLoanMonth = 0;
                double taxAndInsurValue = 0.0;
                double monthlyPayValue = 0.0;

                // Borrowed amount processing
                borrowedAmtValue = Double.parseDouble(borrowedAmt.getText().toString());

                // Interest Rate processing
                rateValue = interestRate.getProgress() / 1200;

                // Loan Term processing
                int[] termGroup = {180, 240, 360};
                int selectedTerm = loanTerm.getCheckedRadioButtonId();
                noOfLoanMonth = termGroup[selectedTerm];

                // Tax and Insurance Included processing
                if (taxAndInsur.isChecked()) 
                    taxAndInsurValue = borrowedAmtValue * 0.001;
                else 
                    taxAndInsurValue = 0.0;

                //making the keyboard disappear after clicking the button
                InputMethodManager objInputMM = (InputMethodManager) getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                objInputMM.hideSoftInputFromWindow(termChosen.getWindowToken(), 0);

                //Calculating total interest value 
                monthlyPayValue = borrowedAmtValue * (rateValue / (1 - Math.pow(1 + rateValue, - noOfLoanMonth)));

                // setting the result into the textview's text property to display to the user
                monthlyPay.setText(Double.toString(monthlyPayValue));
                //txtVwCalculatedInterest.setBackgroundColor(Color.parseColor("#FFCDEF"));

                // resetting text boxes
                //loanAmt.setText("");
                //loanTenure.setText("");
            }
        });
    }

Here is my XML file:

<?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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/enter_borrowed_amt"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/choose_int_rate"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:progress="10"
        android:max="20"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/choose_term"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="clip_vertical"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="@string/yr15" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/yr20" />
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/yr30" />
    </RadioGroup>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/check_tni" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/calculate" 
        android:onClick="setOnClickListener"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/monthly_pay"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="50sp" />

</LinearLayout>

When I run the app on emulator, I realize when I click the button, that the logcat window brings up tons of red lines, so I think there is something wrong here but I don't know how to detect it. Please help me. Thanks much!

My catlog: https://www.dropbox.com/s/ab0m7y05dzzsc9u/Untitled.jpg?dl=0

You defined android:onClick="setOnClickListener" at your Button in your activity_main layout so you need to implement setOnClickListener a method in your Activity just like:

public void setOnClickListener(View v) {

///do your job

}

or another way is just remove android:onClick="setOnClickListener" from layout button and implement onClickListener as

    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
           //do your job
       }
   });

Just remove the android:onClick attribute from Button from your xml layout

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/calculate" />

Updated

I found the issue what you doing

// Loan Term processing
int[] termGroup = {180, 240, 360};
int selectedTerm = loanTerm.getCheckedRadioButtonId();
int selectedTermIndex = 0;
noOfLoanMonth = termGroup[selectedTerm];

Here is 3 line you getting error at last line. In termGroup[] is starting from 0 index to 1,2 now loanTerm.getCheckedRadioButtonId(); will giving you RadioButton reference ID and it may be something like 392032489 etc which is generated in R.java file. You need to get which position of RadioButton is selected like this way.

switch(selectedTerm){
case R.id.radiobtn1:
    selectedTermIndex = 0;
    break;
case R.id.radiobtn2:
    selectedTermIndex = 1;
    break;
case R.id.radiobtn3:
    selectedTermIndex = 2;
    break;
}

Now use selectedTermIndex instead of selectedTerm

noOfLoanMonth = termGroup[selectedTermIndex];

Hope this will be work for you

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