我的应用在执行乘法和除法运算时崩溃

时间:2018-07-26 07:19:48

标签: android

我是android应用程序开发的初学者,我试图制作一个基本的计算器应用程序作为我的第一个项目。 XML代码和JAVA代码如下。

在执行三个操作,即减法乘法除法时,我无法找到错误。

减法期间:发生加法。

乘法期间:应用程序崩溃。

JAVA代码

package com.example.luci4.basic_calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.*;

public class MainActivity extends AppCompatActivity {

    private Button one;
    private Button two;
    private Button three;
    private Button four;
    private Button five;
    private Button six;
    private Button seven;
    private Button eight;
    private Button nine;
    private Button zero;
    private Button add;
    private Button sub;
    private Button mul;
    private Button div;
    private Button equals;
    private Button clear;
    private Button dot;

    private TextView text;

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

        setupUIViews();

        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString()+"0");
            }
        });

        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "1");
            }
        });

        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "2");
            }
        });

        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "3");
            }
        });

        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "4");
            }
        });

        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "5");
            }
        });

        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "6");
            }
        });

        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "7");
            }
        });

        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "8");
            }
        });

        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "9");
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "+" );
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "-" );
            }
        });

        mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "*" );
            }
        });

        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "/" );
            }
        });

        dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText(text.getText().toString() + "." );
            }
        });

        equals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String temp = text.getText().toString();
                if(temp!=null){
                    valueCalculation(temp);
                }
            }
        });
    }

    private void valueCalculation(String s){

        int[] ans = new int[2];
        s = s+" ";
        String temp="";
        char ch = ' ',operator=' ';
        for(int i=0;i<s.length();i++){
            ch=s.charAt(i);
            if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
                ans[0]=Integer.parseInt(temp);
                temp="";
                operator = ch;
            }
            if(ch==' '){
                ans[1]=Integer.parseInt(temp);
            }
            temp+=ch;
        }

        switch(operator){
            case '+':
                text.setText(Integer.toString(add(ans[0],ans[1])));
                break;
            case '-':
                text.setText(Integer.toString(sub(ans[0],ans[1])));
                break;
            case '*':
                text.setText(Integer.toString(mul(ans[0],ans[1])));
                break;
            case '/':
                text.setText(Integer.toString(div(ans[0],ans[1])));
                break;
            default:
                text.setText("Operation Unavailable");
        }
    }


    private void setupUIViews(){
        one = (Button)findViewById(R.id.one);
        two = (Button)findViewById(R.id.two);
        three = (Button)findViewById(R.id.three);
        four = (Button)findViewById(R.id.four);
        five = (Button)findViewById(R.id.five);
        six = (Button)findViewById(R.id.six);
        seven = (Button)findViewById(R.id.seven);
        eight = (Button)findViewById(R.id.eight);
        nine = (Button)findViewById(R.id.nine);
        zero = (Button)findViewById(R.id.zero);
        dot = (Button)findViewById(R.id.dot);
        add = (Button)findViewById(R.id.add);
        sub = (Button)findViewById(R.id.sub);
        mul = (Button)findViewById(R.id.mul);
        div = (Button)findViewById(R.id.div);
        clear = (Button)findViewById(R.id.clear);
        equals = (Button)findViewById(R.id.equals);

        text = (TextView)findViewById(R.id.text);
    }

    private int add(int a,int b){
        int sum = a+b;
        return sum;
    }
    private int sub(int a,int b){
        int dif = a-b;
        return dif;
    }
    private int mul(int a,int b){
        int mul = a*b;
        return mul;
    }
    private int div(int a,int b){
        int div = a/b;
        return div;
    }
}

XML代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.luci4.basic_calculator.MainActivity">

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="117dp"
        android:backgroundTint="@android:color/black"
        android:cursorVisible="false"
        android:ems="30"
        android:inputType="textPersonName"
        android:textColor="@android:color/black"
        android:textColorHighlight="@android:color/black"
        android:textColorLink="@android:color/black"
        android:textSize="35sp" />

    <Button
        android:id="@+id/nine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/text"
        android:layout_marginTop="89dp"
        android:text="9" />

    <Button
        android:id="@+id/eight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/nine"
        android:layout_alignBottom="@+id/nine"
        android:layout_marginStart="15dp"
        android:layout_toEndOf="@+id/nine"
        android:text="8" />

    <Button
        android:id="@+id/seven"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/eight"
        android:layout_alignBottom="@+id/eight"
        android:layout_marginStart="15dp"
        android:layout_toEndOf="@+id/eight"
        android:text="7" />

    <Button
        android:id="@+id/six"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/nine"
        android:layout_marginTop="20dp"
        android:text="6" />

    <Button
        android:id="@+id/five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/six"
        android:layout_alignBottom="@+id/six"
        android:layout_alignStart="@+id/eight"
        android:text="5" />

    <Button
        android:id="@+id/four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/seven"
        android:layout_alignTop="@+id/five"
        android:text="4" />

    <Button
        android:id="@+id/three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/six"
        android:layout_below="@+id/six"
        android:layout_marginTop="20dp"
        android:text="3" />

    <Button
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/five"
        android:layout_alignTop="@+id/three"
        android:text="2" />

    <Button
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/two"
        android:layout_alignStart="@+id/four"
        android:text="1" />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seven"
        android:layout_alignParentEnd="true"
        android:text="+" />

    <Button
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/four"
        android:layout_alignStart="@+id/add"
        android:text="-" />

    <Button
        android:id="@+id/mul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/one"
        android:layout_alignBottom="@+id/one"
        android:layout_alignParentEnd="true"
        android:text="x " />

    <Button
        android:id="@+id/div"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/mul"
        android:layout_marginTop="20dp"
        android:text="/" />

    <Button
        android:id="@+id/zero"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/div"
        android:layout_alignEnd="@+id/one"
        android:text="0" />

    <Button
        android:id="@+id/dot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/two"
        android:layout_alignTop="@+id/zero"
        android:text="." />

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/dot"
        android:text="CLEAR" />

    <Button
        android:id="@+id/equals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:text="=" />

</RelativeLayout>

崩溃日志:

    07-24 19:35:40.075 4978-4978/? I/art: Late-enabling -Xcheck:jni
07-24 19:35:40.076 4978-4978/? W/art: Unexpected CPU variant for X86 using defaults: x86
07-24 19:35:40.106 4978-4985/? E/art: Failed sending reply to debugger: Broken pipe
07-24 19:35:40.106 4978-4985/? I/art: Debugger is no longer active
07-24 19:35:40.106 4978-4985/? I/art: Starting a blocking GC Instrumentation
07-24 19:35:40.212 4978-4978/? W/System: ClassLoader referenced unknown path: /data/app/com.example.luci4.basic_calculator-2/lib/x86
07-24 19:35:40.222 4978-4978/? I/InstantRun: starting instant run server: is main process
07-24 19:35:40.313 4978-4978/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

                                      [ 07-24 19:35:40.532  4978: 4978 D/         ]
                                      HostConnection::get() New Host Connection established 0xd8c9cf20, tid 4978
07-24 19:35:40.823 4978-4994/? D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
07-24 19:35:40.827 4978-4994/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
07-24 19:35:40.831 4978-4994/? D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
07-24 19:35:40.835 4978-4994/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-24 19:35:40.861 4978-4994/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

                                         [ 07-24 19:35:40.887  4978: 4994 D/         ]
                                         HostConnection::get() New Host Connection established 0xd8c9d040, tid 4994
07-24 19:35:40.963 4978-4994/? I/OpenGLRenderer: Initialized EGL, version 1.4
07-24 19:35:40.963 4978-4994/? D/OpenGLRenderer: Swap behavior 1
07-24 19:35:41.011 4978-4978/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
07-24 19:35:58.229 4978-4978/com.example.luci4.basic_calculator D/AndroidRuntime: Shutting down VM
07-24 19:35:58.229 4978-4978/com.example.luci4.basic_calculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.luci4.basic_calculator, PID: 4978
                                                                                  java.lang.NumberFormatException: For input string: "*1"
                                                                                      at java.lang.Integer.parseInt(Integer.java:510)
                                                                                      at java.lang.Integer.parseInt(Integer.java:556)
                                                                                      at com.example.luci4.basic_calculator.MainActivity.valueCalculation(MainActivity.java:169)
                                                                                      at com.example.luci4.basic_calculator.MainActivity.access$100(MainActivity.java:10)
                                                                                      at com.example.luci4.basic_calculator.MainActivity$16.onClick(MainActivity.java:149)
                                                                                      at android.view.View.performClick(View.java:5609)
                                                                                      at android.view.View$PerformClick.run(View.java:22259)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:1)

如@AbhayKoradiya所说

  

“ * 1”错误。您不能将其转换为整数。
  *还添加到临时变量中。那问题。

else添加到您的结构中应该会有所帮助,因此不会每次执行temp+=ch;

if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
    ans[0]=Integer.parseInt(temp);
    temp="";
    operator = ch;
}
else if(ch==' '){ // added here
    ans[1]=Integer.parseInt(temp);
}
else{ // and here
    temp+=ch;
}

我还没有对此进行测试,所以如果有问题,请提供反馈