我的计算器应用程序的java代码在android studio中制作。它崩溃了

时间:2015-07-01 18:29:14

标签: android

    package com.example.android.mycalculator;

    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;


    public class MainActivity extends ActionBarActivity {
        int qty = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

 String val = ((EditText)findViewById(R.id.input1)).getText().toString();
        int num1 = Integer.valueOf(val);
        String val1 = ((EditText)findViewById(R.id.input2)).getText().toString();
        int num2 = Integer.valueOf(val1);
        }
        private void display(int number) {
            TextView quantityTextView = (TextView) findViewById(
                    R.id.quantity_text_view);
            quantityTextView.setText("" + number);
        }
    /*for getting the input values into some variable**/



    /* for adding the input numbers**/
        public void add(View view){
            qty = num1 + num2;
            display(qty);

        }
/*for subtracting two input numbs**/
        public void subtract(View view){
            qty = num1 - num2;
            display(qty);
        }
        public void divide(View view){
            qty = num1 / num2;
            display(qty);
        }
        public void multiply(View view){
            qty = num1 * num2;
            display(qty);
        }
    }

当我通过android studio在我的设备上运行它时,代码崩溃了。 我对代码有疑问,因为我是编码的初学者。请在其中找到错误。程序接受两个输入数字并执行添加,减法,除法和乘法等操作。然后它将输出显示为下面的TextView。程序在计算机中运行良好但在设备上崩溃。跑步时没有显示任何错误。

2 个答案:

答案 0 :(得分:0)

String val = ((EditText)findViewById(R.id.input1)).getText().toString();

你必须转到onCreate

答案 1 :(得分:0)

欢迎来到StackOverFlow.com!

我建议您每次提出问题时如果有任何异常,请尝试发布LogCat中显示的消息: 的 http://developer.android.com/tools/debugging/debugging-log.html

必须要求对基础知识进行简要说明= P. 添加一个方法来设置EditTexts setValues()

中定义的值

此代码将根据您的代码在android中执行计算器:

public class MainActivity extends ActionBarActivity {

    /*for getting the input values into some variable**/
    String val;
    private int num1;
    String val1;
    private int num2;

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

    }
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(
                R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }


    public void setValues(){   
        try{                      
            val = ((EditText)findViewById(R.id.input1)).getText().toString();
            num1 = Integer.valueOf(val);
            val1 = ((EditText)findViewById(R.id.input2)).getText().toString();
            num2 = Integer.valueOf(val1);
        }catch(Exception e){
            Toast.makeText(getApplicationContext(), "Please check your values!", Toast.LENGTH_LONG).show();
        }
    }          

    /* for adding the input numbers**/
    public void add(View view){
        setValues();
        qty = num1 + num2;
        display(qty);
    }       

    /*for subtracting two input numbs**/
    public void substract(View view){
        setValues();
        qty = num1 - num2;
        display(qty);
    }
    public void divide(View view){
        setValues();
        qty = num1 / num2;
        display(qty);
    }
    public void multiply(View view){
        setValues();
        qty = num1 * num2;
        display(qty);
    }
}