본문 바로가기

Android

Android) java.lang.ClassCastException: 오류

728x90
반응형

 Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to com.google.android.material.switchmaterial.SwitchMaterial

오류

 

앱 run은 성공적으로 되는데 어플을 누르면 실행이 안됨

 

해결방법)

 

정말 간단한 실수였다

main.xml에서

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/logout"

으로 지정해두고 main.kt에서

val logout : SwitchMaterial = findViewById(R.id.logout)
logout.setOnCheckedChangeListener { buttonView, _ ->
    if(buttonView.id == R.id.logout){
        //스위치 켜졌을 때
    } else {
        //스위치 꺼졌을 때
    }
}

Button 형식이 아닌 SwitchMaterial으로 선언해두었음.

 

수정)

val logout : Button = findViewById(R.id.logout)
logout.setOnClickListener {
    val intent = Intent(this, sub::class.java)
    startActivity(intent)
}

Button형식으로 수정해주니 정상적으로 앱 실행이 되었다.

728x90
반응형

'Android' 카테고리의 다른 글

Android) 앱 실행 시 화면에 글자 안 뜸: 오류  (0) 2023.08.10