마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오 버튼 안눌림

0 추천
코드는 맞게 짠거 같은데 실행했을때 버튼을 누르면 내용이 실행이 안되요...

도와주세요 ㅠㅠ 원인을 진짜 모르겠어요...

코드는 댓글에 남겨두겠습니다..!
(330 포인트) 님이 2021년 7월 2일 질문
<XML 코드>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/tap1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="8dp"
            android:text="홈"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tap2"
            app:layout_constraintHorizontal_bias="0.44"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/tap2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:text="급여"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tap3"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/tap3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="일정"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tap4"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/tap4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:text="부서"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <ImageView
        android:id="@+id/mainlogo3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/__" />

    <ScrollView
        android:id="@+id/me_Scroll"
        android:layout_width="370dp"
        android:layout_height="270dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mainlogo3">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ListView
                android:id="@+id/me_ScrollList"
                android:layout_width="match_parent"
                android:layout_height="251dp" />
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/me_No"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginEnd="80dp"
        android:layout_marginBottom="13dp"
        android:text="취소"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/me_Yes"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginStart="80dp"
        android:layout_marginBottom="13dp"
        android:text="확인"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/me_DateView"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="24dp"
        android:text="날짜 : "
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/me_Scroll" />

    <TextView
        android:id="@+id/me_StartView"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:text="출근 : "
        app:layout_constraintStart_toStartOf="@+id/me_DateView"
        app:layout_constraintTop_toBottomOf="@+id/me_DateView" />

    <TextView
        android:id="@+id/me_EndView"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:text="퇴근 : "
        app:layout_constraintStart_toStartOf="@+id/me_StartView"
        app:layout_constraintTop_toBottomOf="@+id/me_StartView" />

    <TextView
        android:id="@+id/me_ReasonView"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:text="수정 사유 : "
        app:layout_constraintStart_toStartOf="@+id/me_EndView"
        app:layout_constraintTop_toBottomOf="@+id/me_EndView" />

    <EditText
        android:id="@+id/me_Date"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:ems="10"
        android:inputType="date"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/me_DateView"
        app:layout_constraintTop_toBottomOf="@+id/me_Scroll" />

    <EditText
        android:id="@+id/me_Start"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:ems="10"
        android:inputType="time"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/me_StartView"
        app:layout_constraintTop_toBottomOf="@+id/me_Date" />

    <EditText
        android:id="@+id/me_End"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:ems="10"
        android:inputType="time"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/me_EndView"
        app:layout_constraintTop_toBottomOf="@+id/me_Start" />

    <EditText
        android:id="@+id/me_Reason"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/me_ReasonView"
        app:layout_constraintTop_toBottomOf="@+id/me_End" />

</androidx.constraintlayout.widget.ConstraintLayout>
<Activity 코드>
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import static android.R.layout.activity_list_item;

public class EditActivity extends AppCompatActivity implements View.OnClickListener{
    ListView me_ScrollList;
    EditText me_Date;
    EditText me_Start;
    EditText me_End;
    EditText me_Reason;
    Button me_Yes;
    Button me_No;

    DBHelper helper;
    SQLiteDatabase db;

    Button tap1;
    Button tap2;
    Button tap3;
    Button tap4;

    private void bindViews() {
        me_ScrollList = (ListView)findViewById(R.id.me_ScrollList);
        me_Date = (EditText)findViewById(R.id.me_Date);
        me_Start = (EditText)findViewById(R.id.me_Start);
        me_End = (EditText)findViewById(R.id.me_End);
        me_Reason = (EditText)findViewById(R.id.me_Reason);
        me_Yes = (Button)findViewById(R.id.me_Yes);
        me_No = (Button)findViewById(R.id.me_No);
        tap1 = (Button)findViewById(R.id.tap1);
        tap2 = (Button)findViewById(R.id.tap2);
        tap3 = (Button)findViewById(R.id.tap3);
        tap4 = (Button)findViewById(R.id.tap4);
    }

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

        tap1.setOnClickListener(this);
        tap2.setOnClickListener(this);
        tap3.setOnClickListener(this);
        tap4.setOnClickListener(this);
        selectEdit();
    }

    @Override
    public void onClick(View v) {
        String Date = me_Date.getText().toString();
        String Start = me_Start.getText().toString();
        String End = me_End.getText().toString();
        String Reason = me_Reason.getText().toString();

        if(v==me_Yes) { // 수정 기록 갱신
            if(Date==null||Date==""){
                Toast.makeText(this, "날짜를 입력해주세요.", Toast.LENGTH_SHORT).show();
            }else if(Start==null||Start==""){
                Toast.makeText(this, "출근 시간을 입력해주세요.", Toast.LENGTH_SHORT).show();
            }else if(End==null||End==""){
                Toast.makeText(this, "퇴근 시간을 입력해주세요.", Toast.LENGTH_SHORT).show();
            }else if(Reason==null||Reason==""){
                Toast.makeText(this, "수정 사유를 입력해주세요.", Toast.LENGTH_SHORT).show();
            }else {
                DBHelper helper = new DBHelper(this);
                SQLiteDatabase db = helper.getWritableDatabase();
                db.execSQL("UPDATE cworktbl SET wChange = ?, wChangeDate = ?, wChangeCause = ?, wNewGoTime = ?, wNewOffTime = ? WHERE wDate = ?;",
                        new String[] {"1", Date, Reason, Start, End, Date});
                db.close();

                finish();
            }
        }else if(v==me_No||v==tap1){
            Intent intent1 = new Intent(this, MainActivity.class);
            startActivity(intent1);
        }else if(v==tap2){
            Intent intent2 = new Intent(this, MainActivity.class);
            startActivity(intent2);
        }else if(v==tap3){
            Intent intent3 = new Intent(this, MainActivity.class);
            startActivity(intent3);
        }else if(v==tap4){
            Intent intent4 = new Intent(this, DepartmentActivity.class);
            startActivity(intent4);
        }
    }
    // work SELECT문
    public void selectEdit(){
        try{
            helper = new DBHelper(this);
            db = helper.getWritableDatabase();
            Cursor cursor = db.rawQuery("SELECT wGoTime, wOffTime FROM cworktbl", null);
            MainAdapter adapter = new MainAdapter();
            if(cursor.getCount()!=0){
                //조회온 데이터가 있을때 내부 수행
                while(cursor.moveToNext()) {
                    adapter.addItemList(cursor.getString(0), cursor.getString(1));
                }
            }
            me_ScrollList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }catch(Exception e){e.printStackTrace();}
    }
}
버튼은 Yes버튼과 No버튼이 안눌립니다 ㅠㅠ

1개의 답변

+1 추천
 
채택된 답변

먼저 onClick 메소드 안을 디버깅 해보세요. 그리고 아래가 의심됩니다.

자바에서 String 비교는 equals입니다. 

private String date = m_Date.getText().toString(); // date변수는 lower case로 사용하세요!!! 클래스이름과 중복됩니다.

if(date==null || date.equlas("")) {
   ...
}

 

 


참고로 코드를 읽기 편하게 정리하시길 추천드려요. 변수이름들도 그렇고 읽기에 좀 어지럽네요. 아래를 참고하세요.

public class EditActivity extends AppCompatActivity{
    ListView scrollList; //자바 명명규칙을 따르세요. 변수명에 underscore(_)는 사용하지 않습니다.
    EditText timeSheetDateEdit';
    EditText startDateEdit;
    EditText endDateEdit;
    EditText reasonEdit;
    Button yesButton;
    Button noButton;

    // 마찬가지로, 명확한 이름을 주세요.
    Button homeButton; 
    Button salaryButton;
    Button schduleButton;
    Button departmentButton;

   DBHelper dbHelper;
   SQLiteDatabase db;
   private DBHelper getDBHelper() {
        if (dbHelper == null) {
           dbHelper = new DBHelper(this);
        }
        return dbHelper;
   }

   private SQLiteDatabase getDB() {
        if (db == null) {
           db = getDBHelper().getWritableDatabase();
        }
       return db;
   }

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

        bindViews();       
        fetchTimesheet();
    }
    
    private void bindViews() {
        // XML의 id도 변수이름과 같이 명확하게 주세요.
        scrollList = (ListView)findViewById(R.id.me_ScrollList);
        timeSheetDateEdit = (EditText)findViewById(R.id.me_Date); 
        startDateEdit = (EditText)findViewById(R.id.me_Start);
        endDateEdit = (EditText)findViewById(R.id.me_End);
        reasonEdit = (EditText)findViewById(R.id.me_Reason);

        yesButton = (Button)findViewById(R.id.me_Yes);
        noButton = (Button)findViewById(R.id.me_No);

        // View의 이벤트 핸들러는 개별적으로 사용하는 것이 코드를 읽거나 유지보수하는데 더 쉽습니다.
        yesButton.setOnClickListener { yesButtonClicked() }
        noButton.setOnClickListener { noButtonClicked() }

        homeButton = (Button)findViewById(R.id.tap1);
        salaryButton = (Button)findViewById(R.id.tap2);
        schduleButton = (Button)findViewById(R.id.tap3);
        departmentButton = (Button)findViewById(R.id.tap4);

        homeButton.setOnClickListener { navigateHome(); }
        salaryButton.setOnClickListener { navigateToSalary(); }             
        schduleButton.setOnClickListener { navigateToSchedule(); }
        departmentButton.setOnClickListener{ navigateToDepartment(); }
    }

   // navigation을 하는 코드는 별도로 관리 클래스를 만들어 사용하는 것이 좋습니다.
   // eg. screensNavigator.goHome()
   private void navigateHome() {
       Intent intent1 = new Intent(this, MainActivity.class);
       startActivity(intent1);   
   }
   
   private void navigateToSalary() {
       Intent intent2 = new Intent(this, MainActivity.class);
       startActivity(intent2);
   }

   private void navigateToSchedule() {
       Intent intent3 = new Intent(this, MainActivity.class);
       startActivity(intent3);  
   }
   
   private void navigateToDepartment() {
       Intent intent4 = new Intent(this, DepartmentActivity.class);
       startActivity(intent4);   
   }

   // 폼 유효성 체크를 하는 코드도 별도의 클래스 분리할 수 있다면 더 좋습니다.
   @Nullable
   private FieldError checkUserInputError() {
        List<Pair<String, Int>> inputFields = Arrays.asList(
              new Pair(TextViewUtiils.text(timeSheetDateEdit),  R.string.timesheet_date_label),
              new Pair(TextViewUtiils.text(startDateEdit),  R.string.start_date_label),
              new Pair(TextViewUtiils.text(endDateEdit),  R.string.end_date_label),
              new Pair(TextViewUtiils.text(reasonEdit),  R.string.reason_label)
        );

        for (Pair<String, Int> fieldInfo : inputFields) {
               FieldError error = checkFieldError(fieldInf);
               if (error != null) {
                    return error;
               }
        }

        return null;
   }

   @Nullable
   private FieldError checkFieldError(Pair<String, Int> fieldInfo) {
        return input.isEmpty(fieldInfo.getFirst()) ?: new FieldError(fieldInfo.getSecond()) : null
   }

   private void yesButtonClicked() {
        FieldError error = checkUserInputError();
        if (error != null) {
               showInputErrorMessage(error.getString(this));
               return;
        }
   
        updateTimesheet();
   } 

   private void showInputErrorMessage(String message) {
      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   }

  private void updateTimesheet() {
      getDB().execSQL("UPDATE cworktbl SET wChange = ?, wChangeDate = ?, wChangeCause = ?, wNewGoTime = ?, wNewOffTime = ? WHERE wDate = ?;",
                        new String[] {"1", Date, Reason, Start, End, Date});
      getDB()..close();

      finish();
  }
  
   private void noButtonClicked() {
        ...
   }

    public void fetchTimesheet(){
        try{
            Cursor cursor = getDB().rawQuery("SELECT wGoTime, wOffTime FROM cworktbl", null);
            MainAdapter adapter = new MainAdapter();
            if(cursor.getCount()!=0){
                //조회온 데이터가 있을때 내부 수행
                while(cursor.moveToNext()) {
                    adapter.addItemList(cursor.getString(0), cursor.getString(1));
                }
            }
            me_ScrollList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }catch(Exception e){e.printStackTrace();}
    }
}


// 필드 에러정보를 담는 클래스. 메세지는 가급적이면 하드코딩하지 마시고 String resource 를 사용하세요.
class FieldError {
    @StringRes private final int fieldNameRes;
    @StringRes private final int messageRes; // String resource에 %1$s가 들어가 있어서 포맷팅이 되도록.
    
    public FieldError(int fieldNameRes, int messageRes) {
         this.fieldNameRes = fieldNameRes;
         this.messageRes = messageRes;
    }

    public FieldError(int fieldNameRes) {
       this(fieldNameRes, R.string.field_required_message);
    }

    public String getMessage(context Context) {
        String fieldName = context.getResources().getString(fieldNameRes);
        return context.getResources().getString(messageRes, fieldName);
    }
}

public classTextViewUtils {
      public static String text(TextView textView, defaultValue: String) {
           val s = textView.getText().toString();
           return s == null ? "" : s;
      }

      public static String text(TextView textView) {
          return text(textView, "");
      }
}

 

아직 수정할 부분이 많이 남아 있지만 전체 구조를 잡는데는 도움이 되실 거예요.

spark (188,520 포인트) 님이 2021년 7월 3일 답변
님이 2021년 7월 3일 채택됨
헉 정말 감사합니다ㅠㅠ 조언도 정말 감사합니다!!
...