inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

묻고 답해요

173만명의 커뮤니티!! 함께 토론해봐요.

findViewById 함수 호출 시 null값 반환

미해결

안녕하세요. CustomAdapter 강의 수강중에 다음과 같은 에러가 발생해서 문의드립니다. 그대로 따라하면서 진행하였는데 이상하다 싶어서 에러 메시지를 확인해보니, 아래 코드에서 rowtextview1 변수에 null이 반환되고 있음을 확인하였습니다. val rowtextview1 = findViewById<TextView>(R.id. rowtextView1 ) 전체 코드는 아래와 같습니다. 어떻게하면 View의 id값을 정상적으로 가져올 수 있을까요?? * MainActivity.kt * package com.example.customadapter import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.Button import android.widget.ListView import android.widget.TextView class MainActivity : AppCompatActivity() { val data1 = arrayOf ( " 데이터 1" , " 데이터 2" , " 데이터 3" , " 데이터 4" , " 데이터 5" ) override fun onCreate (savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout. activity_main ) val textveiw1 = findViewById<TextView>(R.id. textView1 ) val listveiw1 = findViewById<ListView>(R.id. list1 ) listveiw1. adapter = adapter1 } //getCount 의 리턴 값 만큼 getView 가 호출되어 R.layout.row 의 View 객체를 붙여준다 . val adapter1 = object : BaseAdapter(){ // 항목의 개수를 반환 override fun getCount (): Int { return data1 . size } // 항목의 인덱스 번호를 전달했을 때 , 객체를 넘기는 기능 override fun getItem (position: Int): Any? { return null } // 임의로 정해진 아이디 값을 넘겨주는 기능 override fun getItemId (position: Int): Long { return 0 } // 현재 화면에 표시되는 항목만큼 호출됨 // position : 몇 번째 항목을 구성할 것인가 , convertView : 화면에서 없어진 뷰를 저장 override fun getView (position: Int , convertView: View? , parent: ViewGroup?): View { // 재사용 가능한 View 를 변수에 담는다 . var rowView = convertView if (rowView == null ) { // inflate 는 XML 을 참고하여 View 객체를 생성 및 반환하는 함수 // 1 번째 인자는 View 객체를 만들 때 참고하는 XML, 2 번째 인자는 해당 View 객체의 상위 객체 ( 레이아웃 )\ rowView = layoutInflater .inflate(R.layout. row , null ) } // 항목 뷰 내부에 배치되어 있는 뷰들의 주소 값을 가져온다 . val rowtextview1 = findViewById<TextView>(R.id. rowtextView1 ) rowtextview1. text = data1 [position] return rowView!! } } } * activity_main.xml * <? 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 ="match_parent" android :layout_height ="match_parent" android :orientation ="vertical" tools :context =".MainActivity" > <TextView android :id ="@+id/textView1" android :layout_width ="match_parent" android :layout_height ="wrap_content" android :text ="TextView" android :textAppearance ="@style/TextAppearance.AppCompat.Large" /> <ListView android :id ="@+id/list1" android :layout_width ="match_parent" android :layout_height ="match_parent" /> </LinearLayout> * row.xml * <? xml version ="1.0" encoding ="utf-8" ?> <LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android" android :layout_width ="match_parent" android :layout_height ="match_parent" android :orientation ="horizontal" > <TextView android :id ="@+id/rowtextView1" android :layout_width ="wrap_content" android :layout_height ="40dp" android :layout_weight ="1" android :text ="TextView" android :textAppearance ="@style/TextAppearance.AppCompat.Large" /> <Button android :id ="@+id/rowbutton1" android :layout_width ="wrap_content" android :layout_height ="wrap_content" android :text =" 버튼 1" /> <Button android :id ="@+id/rowbutton2" android :layout_width ="wrap_content" android :layout_height ="wrap_content" android :text =" 버튼 2" /> </LinearLayout>

  • 윤재성의Kotlin기반안드로이드
  • 안드로이드
  • 코틀린
  • 모바일
  • android
  • 코딩
CNKGH 댓글 1 좋아요 0 조회수 1105

인기 태그

인프런 TOP Writers

주간 인기글