코드는 이건데 숫자 입력후 더하기 누르면 강제종료되여,,,,뭘까요 선배님들 ㅠ_ㅠ
<?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.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 onCreate(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() {
@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() {
@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() {
@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() {
@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;
}
});
}}
정확한 원인을 아시려면 디버깅을 하시거나 로그를 찍어보세요. 그리고 버튼에 setOnClickListener 를 안쓰시고 setOnTocuhListener 를 사용하시는 이유가 있나요?
Integer.parseInt(Num1) 는 숫자형태의 값이 들어오지 않으면 예외를 발생시키므로 try catch로 유효성 체크를 하셔야 합니다.