Android 安卓,请告诉我我的错误

Android 安卓,请告诉我我的错误,android,debugging,Android,Debugging,我对Java和Android编程都很陌生。我在试着做一个计算器。但只要我插入平方根按钮,每当我按下除法或平方根时,应用程序就会崩溃。我不知道我错在哪里。请帮忙 public class MainScreen extends Activity implements OnClickListener { EditText edit1, edit2; TextView txt; Button butt, diff, mul, div, sqr, per;

我对Java和Android编程都很陌生。我在试着做一个计算器。但只要我插入平方根按钮,每当我按下除法或平方根时,应用程序就会崩溃。我不知道我错在哪里。请帮忙

public class MainScreen extends Activity implements OnClickListener {
      EditText edit1, edit2;
      TextView txt;
      Button butt, diff, mul, div, sqr, per;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        edit1 = (EditText)findViewById(R.id.editText1);
        edit2 = (EditText)findViewById(R.id.editText2);


        txt = (TextView)findViewById(R.id.textView1);



        butt = (Button)findViewById(R.id.button1);
        butt.setOnClickListener(this);
        diff = (Button)findViewById(R.id.button2);
        diff.setOnClickListener(this);
        mul = (Button)findViewById(R.id.button3);
        mul.setOnClickListener(this);
        div = (Button)findViewById(R.id.button4);
        div.setOnClickListener(this);
        sqr = (Button)findViewById(R.id.button5);
        sqr.setOnClickListener(this);
        per = (Button)findViewById(R.id.button6);
        per.setOnClickListener(this);
        }


    @Override
    public void onClick(View v) {
        String a;
        // TODO Auto-generated method stub
        String b;
        Integer sum, sub, mul, div;
        Double x ;



        a= edit1.getText().toString();
        b= edit2.getText().toString();
        x=(double) Integer.parseInt(a);

        switch (v.getId()) {

            case R.id.button1:
            sum = Integer.parseInt(a)+ Integer.parseInt(b);
            txt.setText(sum.toString());
            break;

            case R.id.button2:
            sub = Integer.parseInt(a)- Integer.parseInt(b);
            txt.setText(sub.toString());
            break;

            case R.id.button3:
            mul = Integer.parseInt(a)* Integer.parseInt(b);
            txt.setText(mul.toString());
            break;

            case R.id.button4:
            div = (Integer.parseInt(a))/(Integer.parseInt(b));
            txt.setText(div.toString());

            case R.id.button5:
            txt.setText((int) Math.sqrt(x));


            default:
            break;
        }}}

您需要检查be的值,以确保没有被0除


您可以发布您收到的错误吗?

您可以通过
$adb logcat
查看错误。此外,在最后两个“案例”中,你没有“突破”

  • 使用
    (int)Math是个坏主意。sqrt(x)
    -sqrt不返回整数

  • 当您编写
    textView.setText(int)
    -Andrid认为,您提供了字符串资源的id,但它找不到它,因此崩溃

  • 使用以下命令:

    case R.id.button5:
        txt.setText(String.valueOf(Math.sqrt(x)));
        break;
    

    包括堆栈跟踪。谢谢Jin,sqrt现在工作得很好。但是divide按钮返回与sqrt按钮相同的结果。我不明白为什么。完全没有连接。你忘记了
    中断
    语句:)接受答案(在答案左边打勾)如果你不介意的话,我可以知道你的电子邮件地址或其他信息吗?在这个网站上提问,如果我知道答案,我会回答:)