-2
    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);
        }
    }

The code got crashed when I ran it on my device through android studio. I have a doubt in the code because I'm a beginner in coding. Pls find the error in it.The program takes two input numbers and performs actions like adding,subtracting,dividing and multiplying. It then shows the output as a TextView below.The program ran well in the computer but crashed on the device. No errors were shown while running.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Where do you actually call any of your functions? – bigC5012 Jul 01 '15 at 18:44
  • I guess you call the methods from the xml onClick. bring the logcat of the error so we could help – yshahak Jul 01 '15 at 18:51
  • [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this/) – nhaarman Jul 01 '15 at 20:16

2 Answers2

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

you have to move to onCreate

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
jBegera
  • 405
  • 2
  • 12
0

Welcome to StackOverFlow.com!

I suggest that every time that you make a question if you have any exception try to post the messages displayed in your LogCat: http://developer.android.com/tools/debugging/debugging-log.html

a brief reading to the basics must be required =P add a method to set the values defined in your EditTexts setValues()

this code will do the job for a calculator in android based on your code:

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);
    }
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268