Akshay Soam
Akshay Soam

Reputation: 1620

Android Calculator App

I'm new to android app development. I was building a basic calculator app in android. I have no compile time errors in the code but when i'm trying to run this app it shows "Unfortunately calculator has stopped working". I have copied XML Layout and .java file one after the other.

`

<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input1"/>

    <EditText 
        android:id="@+id/ed1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/input1"/>
</LinearLayout>

<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input2"/>

    <EditText 
        android:id="@+id/ed3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:hint="@string/input2"/>
</LinearLayout>

<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/result"/>

    <EditText 
        android:id="@+id/ed2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/result"/>
</LinearLayout>

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp" >

    <Button
        android:id="@+id/btn1" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/add"/>

    <Button
        android:id="@+id/btn2" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/sub"/>

</LinearLayout>

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn3" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/mul"/>

    <Button
        android:id="@+id/btn4" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/div"/>

</LinearLayout>
</LinearLayout>









package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
public Integer int3;

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

    final EditText input1=(EditText) findViewById(R.id.ed1);
    final EditText input2=(EditText) findViewById(R.id.ed2);

    final Integer int1=Integer.parseInt(input1.getText().toString());
    final Integer int2=Integer.parseInt(input2.getText().toString());

    Button add=(Button) findViewById(R.id.btn1);
    add.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int3=int1+int2;
        }
    });

    Button sub=(Button) findViewById(R.id.btn2);
    sub.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int3=int1-int2;
        }
    });

    Button mul=(Button) findViewById(R.id.btn3);
    mul.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int3=int1*int2;
        }
    });

    Button div=(Button) findViewById(R.id.btn4);
    div.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            int3=int1/int2;
        }
    }); 

    final EditText result=(EditText) findViewById(R.id.ed2);
    result.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            result.setText(Integer.toString(int3));
        }
    });
}
}'

Upvotes: 0

Views: 544

Answers (1)

Aung Aung Swe
Aung Aung Swe

Reputation: 63

Do not Add this on onCreate()

  final Integer int1=Integer.parseInt(input1.getText().toString());
  final Integer int2=Integer.parseInt(input2.getText().toString()); )

DELETE IT !!

Otherwise, You have to add this convert sentences when you create onClickListener ! you should add these line in every onClick methods like this.

  add.setOnClickListener(new OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
        Integer int1=Integer.parseInt(input1.getText().toString());
        Integer int2=Integer.parseInt(input2.getText().toString());
        int3=int1+int2;
    }
  });

 Button sub=(Button) findViewById(R.id.btn2);
 sub.setOnClickListener(new OnClickListener()
 {
    @Override
    public void onClick(View v)
     {
        Integer int1=Integer.parseInt(input1.getText().toString());
        Integer int2=Integer.parseInt(input2.getText().toString());
        int3=int1-int2;
     }
 });

There will be no error. Additionally you should consider how about clicking without edittext value, it can cause just the error you got !Actually, you have to void assigning null(space) value to integer. Your error is putting space to integer. Edittext have space value and you are changing it to integer. Your Error !!

Upvotes: 1

Related Questions