버튼을 눌러도 왜 토스트 메시지는 안나올까요??

21.01.01 17:25 작성 조회수 46

0

삭제된 글입니다

답변 10

·

답변을 작성해보세요.

0

핸드폰 연결해서 하면 되네요.. 에뮬레이터가 문제가 있었나봐요

궁금한게 있는데 위에 제가한것처럼 바인딩을 사용하는게 좋을까요? 아니면 강사님처럼 findViewById를 사용하는게 좋을까요?

0

왜그럴까요..? 제가 올려드린 코드로 한번 돌려보시겠어요?

0

지우면 아무일도 일어나지 않아요...

0

아.. 혹시 페이지가 넘어가서 toast가 안뜨는게 아닐까요?

intent부분을 잠시 지우고 하면 어떻게 되나요?

0

이렇게 해도 토스트메시지는 안보이네요

뒤에 intend추가하면 화면 넘어가서 사진은 보여요ㅜㅜ

왜 안나오는지 잘모르겠네요ㅠㅠ

0

PhotoActivity.java 같은 경우에는 아마 작업상의 실수였나봅니다~ 

0

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val testButton = findViewById<Button>(R.id.test)
testButton.setOnClickListener {
Toast.makeText(this, "정국버튼입니다.", Toast.LENGTH_LONG).show()
}
}
}

<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=".MainActivity">

<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

제가 예제를 하나 만들었는데 이걸 참고해보시겠어요?

0

//activity_main.xml

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

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="정국"/>

</LinearLayout>



//activity_photo.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=".PhotoActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo"/>

</androidx.constraintlayout.widget.ConstraintLayout>


빌드시 오류는 없고 intent로 화면 전환 되어서 사진까지는 나옵니다.
PhotoActivity::class.java 말고 저는 액티비티를 만들때
PhotoActivity.kt로 생성했는데 강사님은 PhotoActivity.java로 만드셔서 여쭤본거였어욤

0

binding.button.set ---

이 부분의 button이 xml에 파일에 button이라고 id가 되어 있어야 합니다~ xml파일도 보내주시면 좋을 것 같아요

0

안녕하세요 이야이야님

확인해주셔야 하는 것이

1.혹시 에러가 나오거나, intent로 화면 전환이 이루어지나요??

2. xml파일에는 어떻게 되어 있나요? 

그리고 PhotoActivity::class.java는 화면이 전환되는 문법같은 것이라고 생각해주시면 됩니다.