当我单击“运行”按钮时,应用程序停止运行错误 - Android应用程序

时间:2014-09-17 06:38:33

标签: java android mobile android-activity

我正在编写抵押贷款计算器应用程序。我以为我已经完成了应用程序,它现在应该可以工作了。但是,每次单击“计算每月付款”按钮时,应用程序都会停止并立即退出并显示错误消息“很遗憾,您的应用已停止”

以下是 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("");
            }
        });
    }

这是我的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" >

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

当我在模拟器上运行应用程序时,我意识到当我单击按钮时,logcat窗口会出现大量的红线,所以我认为这里有问题,但我不知道如何检测它。请帮我。非常感谢!

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

2 个答案:

答案 0 :(得分:1)

您在android:onClick="setOnClickListener"布局的Button处定义了activity_main,因此您需要在setOnClickListener中实施Activity方法,如下所示:

public void setOnClickListener(View v) {

///do your job

}

或其他方式只是从布局按钮中移除android:onClick="setOnClickListener"并将 onClickListener 实现为

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

答案 1 :(得分:0)

只需从xml布局中删除Button中的android:onClick属性

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

<强>更新

我发现你正在做的事情

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

这是你在最后一行得到错误的3行。在termGroup []从0索引开始到1,2现在loanTerm.getCheckedRadioButtonId();将为您提供RadioButton引用ID,它可能类似于在R.java文件中生成的392032489等。你需要像这样选择RadioButton的哪个位置。

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

现在使用selectedTermIndex代替selectedTerm

noOfLoanMonth = termGroup[selectedTermIndex];

希望这对你有用