0

After clicking the sum button on emulator, the app has stopped.
But when I wrote the code first time and run it. There was no problem at all!

Java Code:

package com.mateors.add;

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

public class MainActivity extends AppCompatActivity {

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

public void onButtonClick (View v) {

    EditText e1 = (EditText)findViewById(R.id.editText);
    EditText e2 = (EditText)findViewById(R.id.editText2);
    TextView t1 = (TextView)findViewById(R.id.textView);
    int num1 = Integer.parseInt(e1.getText().toString());
    int num2 = Integer.parseInt(e2.getText().toString());
    int sum = num1 + num2;
    t1.setText(Integer.toString(sum));

}
}

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mateors.calculator.MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="18dp"
    android:layout_marginStart="18dp"
    android:layout_marginTop="100dp"
    android:id="@+id/editText"
    android:hint="Number 1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText"
    android:layout_marginTop="48dp"
    android:id="@+id/editText2"
    android:hint="Number 2" />

<Button
    android:text="SUM"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignStart="@+id/editText2"
    android:layout_marginTop="31dp"
    android:id="@+id/button"
    android:layout_alignRight="@+id/editText2"
    android:layout_alignEnd="@+id/editText2"
    android:onClick="onButtonClick (MainActivity)" />

<TextView
    android:text="Result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20dp"
    />

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Bill Redoy
  • 91
  • 2
  • 8
  • 3
    Please provide some error logs – Alex Oct 17 '16 at 06:58
  • 1
    May I ask where you are learning this from? I saw the exact same problem the other day. You can not have spaces or symbols in the `onClick` of the XML – OneCricketeer Oct 17 '16 at 07:12
  • Also "App has stopped" isn't the error message. Here's how you get the error message. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – OneCricketeer Oct 17 '16 at 07:14
  • 10-17 08:00:13.940 1058-1078/system_process I/WindowState: WIN DEATH: Window{a7da22b0 com.mateors.add/com.mateors.add.MainActivity paused=false} 10-17 08:00:13.940 1058-1058/system_process I/ActivityManager: Process com.mateors.add (pid 1717) has died. 10-17 08:00:14.169 1058-1076/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 1717 uid 10045 10-17 08:00:21.860 1058-1094/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{a7b32f18 com.mateors.add/.MainActivity} – Bill Redoy Oct 17 '16 at 08:04

1 Answers1

0

first of all change this xml in Button.

 android:onClick="onButtonClick (MainActivity)" 

to this

 android:onClick="onButtonClick" 

so finally your Button is display like this

<Button
    android:text="SUM"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignStart="@+id/editText2"
    android:layout_marginTop="31dp"
    android:id="@+id/button"
    android:layout_alignRight="@+id/editText2"
    android:layout_alignEnd="@+id/editText2"
    android:onClick="onButtonClick" />
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95