简单兴趣计算器计算按钮错误

时间:2015-04-25 18:57:11

标签: android calculator

我是Android开发的新手,并且一直试图制作一个简单的兴趣计算器。它使用SeekBar来输入输入的年数。单击“计算”按钮时,应用程序崩溃。

我正在使用Eclipse并尝试在没有任何模拟器的情况下直接在设备上运行应用程序。

package com.sevendaytutorials.interestcalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener{

    private EditText principal_amount;
    private EditText rate;
    private SeekBar seekbar;

    private TextView seekBarValue;
    private Button calculate;
    private TextView result;

    private String Principal, Rate;
    private int number_of_years;


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

        principal_amount = (EditText)findViewById(R.id.principal_amount);
        Principal = principal_amount.getText().toString();

        rate = (EditText)findViewById(R.id.interest_rate);
        Rate = rate.getText().toString();

        seekbar = (SeekBar)findViewById(R.id.seekBar1);
        seekbar.setOnSeekBarChangeListener(this);


        seekBarValue = (TextView) findViewById(R.id.number_of_years);



        calculate = (Button) findViewById(R.id.calculate_button);
        calculate.setOnClickListener(this);

        result = (TextView) findViewById(R.id.result_textview);
    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == calculate) {
            Calculate();
        }

    }

    private void Calculate() {
        // TODO Auto-generated method stub
        double p = Double.parseDouble(Principal);
        double R = Double.parseDouble(Rate);
        double r = R/100.0;
        double A = p*(1+r*number_of_years);
        double I = A - p;
        result.setText("The amount of interest after " +
                Integer.toString(number_of_years)+" year(s) would be " + Double.toString(I));

    }



    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        seekBarValue.setText(Integer.toString(progress));
        number_of_years = progress;
    }



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

    }



    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub



    }

2 个答案:

答案 0 :(得分:0)

在你的onCreate()中,你做了这些 -

Principal = principal_amount.getText().toString();

Rate = rate.getText().toString();

现在,由于在null

期间没有输入任何内容,因此您的两个值均为onCreate()

现在你什么时候打电话给Calculate(),你正在做这些 -

double p = Double.parseDouble(Principal);
double R = Double.parseDouble(Rate); 

我们可以看到,您没有为Principal&分配任何适当的值。 Rate但是在任何地方,所以你应该在Exception处获得private void Calculate() { // TODO Auto-generated method stub Principal = principal_amount.getText().toString(); Rate = rate.getText().toString(); if (Principal == null || Principal.isEmpty() || Rate == null || Rate .isEmpty()) { // user are yet to fill the edittext, validation error }else{ double p = Double.parseDouble(Principal); double R = Double.parseDouble(Rate); double r = R/100.0; double A = p*(1+r*number_of_years); double I = A - p; result.setText("The amount of interest after " + Integer.toString(number_of_years)+" year(s) would be " + Double.toString(I)); } } 。你应该修改为 -

EditText

答案 1 :(得分:0)

  

局部变量

SeekBar seek1, seek2, seek3;
float amount, rate, time, interest, total;
TextView tv1, tv2, tv3, tv4, tv5;
Button calculate;
String val, val1, val2, inter, totall;
  

在onCreate()中找到ID

seekBar1 = findViewById(R.id.seek1);
seekBar2 = findViewById(R.id.seek2);
seekBar3 = findViewById(R.id.seek3);
calculate = findViewById(R.id.calculate);
tvAmount = findViewById(R.id.tv1);
tvRate = findViewById(R.id.tv2);
tvtime = findViewById(R.id.tv3);
tvEMI = findViewById(R.id.tv4);
tvInterest = findViewById(R.id.tv5);
  

在按钮上应用setOnClickListener()

calculate.setOnClickListener(this);

  

使用匿名类应用setOnSeekBarChangeListener()并实现   所有方法

seek1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()    
{
  

在字符串变量中获取SeekBar的值并在文本视图上进行设置。

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val = String.valueOf(1000 + i * 1000);
tv1.setText(val);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{

}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
seek2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{                 

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val1 = String.valueOf(5 + i * .5);
tv2.setText(val1);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
seek3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val2 = String.valueOf(i);
tv3.setText(val2);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
  

设置搜索条的最大限制

seek1.setMax(99);
seek2.setMax(30);
seek3.setMax(100);
  

对按钮点击操作实施onClick()

@Override
public void onClick(View view)
{
calInterest();
tv4.setText(": ₹"+inter);
tv5.setText(": ₹"+totall);
}
  

创建用于计算calInterest()的方法,该方法在时间被调用   按钮的点击,请参阅onClick()

public void calInterest()
{
amount = Float.parseFloat(val);
rate = Float.parseFloat(val1);
time = Float.parseFloat(val2);
interest = (amount * rate * (time / 12))/100;
total =interest+amount;
inter = String.valueOf(interest);
totall = String.valueOf(total);
}