按两次等于按钮时计算器应用程序崩溃

时间:2016-01-13 11:04:30

标签: android

我正在调整计算器应用。该应用程序正常执行所有操作,但当我单击两次等于按钮时,应用程序崩溃。

在我的layout中,我将android:onClick ="clicaBotao"用于执行操作的所有按钮,包括等号按钮。

使用clicaBotao方法在MainActivity下方。

public class MainActivity extends AppCompatActivity {

private TextView campoTexto;
float  numeroA = 0;
String operacao = "";

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

    campoTexto = (TextView)findViewById(R.id.campoResultado);
    campoTexto.setText("0");
}

public  void clicaBotao(View view){
    switch (view.getId()){
        case R.id.botaoClear:
            campoTexto.setText("0");
            numeroA=0;
            operacao="";
            break;
        case R.id.botaoSoma:
            calculaNumeros("+");
            break;
        case R.id.botaoSubtrai:
            calculaNumeros("-");
            break;
        case R.id.botaoDivide:
            calculaNumeros("/");
            break;
        case R.id.botaoMultiplica:
            calculaNumeros("*");
            break;
        case R.id.botaoIgual:
            mostraResultado();
            break;
        default :
            String numb;
            numb = ((Button)view).getText().toString();
            getKeyboard(numb);
            break;
    }
}


public void calculaNumeros(String tipoOperacao){
    numeroA = Float.parseFloat(campoTexto.getText().toString());
    operacao = tipoOperacao;
    campoTexto.setText("0");
}


public void getKeyboard(String str)
{
    String ScrCurrent = campoTexto.getText().toString();
    ScrCurrent += str;
    campoTexto.setText(ScrCurrent);
}


public void mostraResultado()
{
    float    numeroB = Integer.parseInt(campoTexto.getText().toString());
    float result = 0;
    if(operacao.equals("+"))
    {
        result = numeroB + numeroA;
    }
    if(operacao.equals("-"))
    {
        result = numeroA - numeroB;
    }
    if(operacao.equals("*"))
    {
        result = numeroB * numeroA;
    }
    if(operacao.equals("/"))
    {
        result = numeroA / numeroB;
    }
    campoTexto.setText(String.valueOf(result));
  }
}

logcat

    01-13 08:43:58.775 3761-3761/titopetri.com.minhacalculadora E/AndroidRuntime: FATAL EXCEPTION: main
Process: titopetri.com.minhacalculadora, PID: 3761
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.lang.NumberFormatException: Invalid int: "14.0"
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:410)
    at java.lang.Integer.parseInt(Integer.java:367)
    at java.lang.Integer.parseInt(Integer.java:334)
    at titopetri.com.minhacalculadora.MainActivity.mostraResultado(MainActivity.java:76)
    at titopetri.com.minhacalculadora.MainActivity.clicaBotao(MainActivity.java:48)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

替换方法mostraResultado()

 float    numeroB = Integer.parseInt(campoTexto.getText().toString());

通过

 float    numeroB = Float.parseFloat(campoTexto.getText().toString());