简体   繁体   English

不幸的是,应用在Android的AVD上停止了

[英]Unfortunately app stopped on AVD in android

I am trying to build a simple calculator app having: 我正在尝试构建一个具有以下功能的简单计算器应用程序:

3 EditText for 2 numbers and for result. 3 EditText用于2个数字和结果。

6 Buttons + - * / = clear. 6个Buttons +-* / =清除。

When running this app on the AVD it is flashing "Unfortunately app stopped!" 在AVD上运行此应用程序时,它闪烁“不幸的是,应用程序已停止!”

That means there would be some sort of exception being encountered but i am not able to understand what. 这意味着将会遇到某种异常,但是我不明白是什么。

When i checked on Logcat, there was something "EditText cannot be typecast to String" . 当我检查Logcat时,出现了“无法将EditText类型转换为String”的信息

Please help me out to debug this exception. 请帮助我调试此异常。

package com.example.calci_ver1;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

Button badd, bsub, bmul, bdiv, beq, bclr;
static EditText et1, et2, et3;

static int i=0, n1, n2;

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

    badd = (Button)findViewById(R.id.button1);
    bsub = (Button)findViewById(R.id.button2);
    bmul = (Button)findViewById(R.id.button3);
    bdiv = (Button)findViewById(R.id.button4);
    beq = (Button)findViewById(R.id.button5);
    bclr = (Button)findViewById(R.id.button6);

    et1 = (EditText)findViewById(R.id.editText1);
    et2 = (EditText)findViewById(R.id.editText2);
    et3 = (EditText)findViewById(R.id.editText3);

    badd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            i = 1;
        }
    });

    bsub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            i = 2;
        }
    });

    bmul.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            i = 3;
        }
    });

    bdiv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            i = 4;
        }
    });

    beq.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            String s1, s2;

            s1 = et1.getText().toString();
            s2 = et2.getText().toString();

            n1 = Integer.parseInt(s1);
            n2 = Integer.parseInt(s2);

            calculation();

            i = 0;
        }
    });

    bclr.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            et1.setText("");
            et2.setText("");
            et3.setText("");
        }
    });
}

public static void calculation() {

    switch(i)
    {
        case 1: et3.setText(n1+n2);
        break;

        case 2: et3.setText(n1-n2);
        break;

        case 3: et3.setText(n1*n2);
        break;

        case 4: et3.setText(n1/n2);
        break;
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

I found problem that may cause application crash.. 我发现了可能导致应用程序崩溃的问题。

Remove the code what you written in calculation() method. 删除您在calculation()方法中编写的代码。

Actually your problem is that you pass the result of two numbers its give the numeric value and android has method on edittext setText(int resid) so its means its searching text of resid in string.xml which is not present. 实际上,您的问题是您传递了两个数字的结果,并给出了数字值,而androidedittext setText(int resid) edittext setText(int resid)上具有方法,因此,这意味着其在string.xml搜索string.xml文本不存在。 So its crashing your application. 因此,它使您的应用程序崩溃。 So you need to convert the result in String . 因此,您需要将结果转换为String

Use this. 用这个。

public static void calculation() {

    switch(i)
    {
        double result = 0;
        case 1: 
            result = n1+n2;
            et3.setText(String.valueOf(result));
        break;

        case 2: 
            result = n1-n2;
            et3.setText(String.valueOf(result));
        break;

        case 3: 
            result = n1*n2;
            et3.setText(String.valueOf(result));
        break;

        case 4: 
            result = n1/n2;
            et3.setText(String.valueOf(result));
        break;
    }
}

I've found AVD occasionally throws those errors when you call a .setText() command, I have never encountered the error on a real life device though, so just reinstall the program on your AVD. 我发现当您调用.setText()命令时,AVD有时会引发这些错误,尽管我从未在实际设备上遇到该错误,所以只需在AVD上重新安装该程序即可。 I'll look around a bit because I had this problem and I know I have seen it on SO before. 我会环顾四周,因为我遇到了这个问题,而且我知道我以前在SO上已经看到过。

Use android:inputType="number" to force it to be numeric. 使用android:inputType =“ number”强制其为数字。

Convert the resulting string into an integer (eg, Integer.parseInt(myEditText.getText().toString())). 将结果字符串转换为整数(例如Integer.parseInt(myEditText.getText()。toString()))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM