안드로이드 실행 오류.

조회수 581회

안녕하세요 계산기 앱을 만들었는데 4 +7을 할라고할때 더하기 누르면 강제종료되요.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

<EditText
android:id="@+id/Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자1"/>

<EditText
    android:id="@+id/Edit2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:hint="숫자2"/>


<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="더하기"/>


<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="빼기"/>


<Button
android:id="@+id/BtnnMuI"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="곱하기"/>

<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="나누기"/>

    <TextView
        android:id="@+id/TextResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="계산결과:"
        android:textColor="#000000"
        android:textSize="30dp"/>


</LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    EditText edit1,edit2;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView textResult;
    String Num1,Num2;
    Integer result;


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


        setTitle("초간단계산기");
        edit1 = (EditText) findViewById(R.id.Edit1);
        edit2 = (EditText) findViewById(R.id.Edit2);
        btnAdd = (Button) findViewById(R.id.BtnAdd);
        btnSub = (Button) findViewById(R.id.BtnSub);
        btnMul = (Button) findViewById(R.id.BtnnMuI);
        btnDiv = (Button) findViewById(R.id.BtnDiv);
        textResult = (TextView) findViewById(R.id.TextResult);


        btnAdd.setOnTouchListener(new
                                          View.OnTouchListener() {
                                              @SuppressLint("ClickableViewAccessibility")
                                              @Override
                                              public boolean onTouch(View v, MotionEvent motionEvent) {
                                                  Num1 = edit1.getText().toString();
                                                  Num2 = edit2.getText().toString();
                                                  result = Integer.parseInt(Num1) +
                                                          Integer.parseInt(Num2);
                                                  textResult.setText("계산결과" + result.toString());
                                                  return false;
                                              }
                                          });

        btnSub.setOnTouchListener(new
                                          View.OnTouchListener() {
                                              @SuppressLint("ClickableViewAccessibility")
                                              @Override
                                              public boolean onTouch(View v, MotionEvent motionEvent) {
                                                  Num1 = edit1.getText().toString();
                                                  Num2 = edit2.getText().toString();
                                                  result = Integer.parseInt(Num1) +
                                                          Integer.parseInt(Num2);
                                                  textResult.setText("계산결과" + result.toString());
                                                  return false;
                                              }
                                          });
        btnMul.setOnTouchListener(new
                                          View.OnTouchListener() {
                                              @SuppressLint("ClickableViewAccessibility")
                                              @Override
                                              public boolean onTouch(View v, MotionEvent motionEvent) {
                                                  Num1 = edit1.getText().toString();
                                                  Num2 = edit2.getText().toString();
                                                  result = Integer.parseInt(Num1) +
                                                          Integer.parseInt(Num2);
                                                  textResult.setText("계산결과" + result.toString());
                                                  return false;
                                              }
                                          });
        btnDiv.setOnTouchListener(new
                                          View.OnTouchListener() {
                                              @SuppressLint("ClickableViewAccessibility")
                                              @Override
                                              public boolean onTouch(View v, MotionEvent motionEvent) {
                                                  Num1 = edit1.getText().toString();
                                                  Num2 = edit2.getText().toString();
                                                  result = Integer.parseInt(Num1) +
                                                          Integer.parseInt(Num2);
                                                  textResult.setText("계산결과" + result.toString());
                                                  return false;
                                              }
                                          });
    }}

어떻게 고쳐야할까요? 값을 입력해야하는데 도무지 모르겠습니다. 선배님들 도와주세요.

  • exception 로그를 첨부해주시면 좋겠습니다만.. 코드만 보았을 때는 Integer.parseInt() 에서 exception 이 발생 했을 가능성이 높아 보이네요. 숫자 입력창에 빈문자열이나 숫자가 아닌 문자가 포함 되어있지 않은지 확인해보시기 바랍니다 알 수 없는 사용자 2021.3.14 14:31

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)