android studio显示没有错误,但代码崩溃

时间:2017-02-19 16:34:22

标签: java android

我是android studio和java的新手,所以请求使句子变得简单。 因为我是新手,所以我想创建一个简单的程序,如计算器(第二个程序)。 代码是:

package com.example.mikus.calc;

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

public class MainActivity extends AppCompatActivity {
    private static int task = 0;
    private static String in1 = "0";
    private static String in2 = "0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1 = (Button) findViewById(R.id.bt1);
        Button bt2 = (Button) findViewById(R.id.bt2);
        Button bt3 = (Button) findViewById(R.id.bt3);
        Button bt4 = (Button) findViewById(R.id.bt4);
        Button bt5 = (Button) findViewById(R.id.bt5);
        Button bt6 = (Button) findViewById(R.id.bt6);
        Button bt7 = (Button) findViewById(R.id.bt7);
        Button bt8 = (Button) findViewById(R.id.bt8);
        Button bt9 = (Button) findViewById(R.id.bt9);
        Button bt0 = (Button) findViewById(R.id.bt0);
        Button btdot = (Button) findViewById(R.id.btdot);
        Button btdevide = (Button) findViewById(R.id.btdevide);
        Button bttimes = (Button) findViewById(R.id.bttimes);
        Button btminus = (Button) findViewById(R.id.btminus);
        Button btplus = (Button) findViewById(R.id.btplus);
        Button btequals = (Button) findViewById(R.id.btequals);
        final TextView txt = (TextView) findViewById(R.id.textView);


        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "1";
                txt.setText(text);
            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "2";

                txt.setText(text);
            }
        });

        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "3";

                txt.setText(text);
            }
        });

        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "4";

                txt.setText(text);
            }
        });

        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "5";

                txt.setText(text);
            }
        });

        bt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "6";

                txt.setText(text);
            }
        });

        bt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "7";

                txt.setText(text);
            }
        });

        bt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "8";

                txt.setText(text);
            }
        });

        bt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "9";

                txt.setText(text);
            }
        });

        bt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + "0";

                txt.setText(text);
            }
        });

        btdot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = txt.getText() + ".";

                txt.setText(text);
            }
        });

        btequals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = Integer.parseInt(in1);
                in2 = txt.getText().toString();
                int b = Integer.parseInt(in2);
                if(task == 1){
                    txt.setText(a + b);
                }
                if(task == 2){
                    txt.setText(a - b);
                }
                if(task == 3){
                    txt.setText(a * b);
                }
                if(task == 4){
                    txt.setText(a / b);
                }
            }
        });

        btplus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task = 1;
                in1 = txt.getText().toString();
                txt.setText("");
            }
        });

        btminus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task = 2;
                in1 = txt.getText().toString();
                txt.setText("");
            }
        });

        bttimes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task = 3;
                in1 = txt.getText().toString();
                txt.setText("");
            }
        });


        btdevide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task = 4;
                in1 = txt.getText().toString();
                txt.setText("");
            }
        });
    }
}

我在我的android工作室编译它没有错误我在我的手机上运行它当我按下等于它只是崩溃。(我只是尝试添加。并且不要告诉我我将无法将小数添加到整数或除法将不精确或其他东西)。顺便说一下,哪个按钮对应于什么名称没有错误。布局与手机上的默认计算器相同。 bt代表按钮,数字或名称描述按钮是什么或它在

中做了什么

1 个答案:

答案 0 :(得分:0)

尝试写:

os.lds

然后你将获得方程的字符串值,并且可以设置为TextView中的文本。