본문 바로가기

Android/TroubleShooting

[Android] Tablayout의 Tabitem 사용시 java.lang.NullPointerException: Missing required view with ID 오류

728x90
반응형

Tablayout의 Tabitem ID를 지정하는 경우 위와 같은 오류가 뜬다.

 

Material Tab에서 사용할 때 발생하는 버그로 tabItem의 ID를 삭제해주면 해결된다.

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

 

 

ID를 사용해서 tablayout에 접근하지 못하므로 tablayout의 api를 사용해야 한다.

val tablayout = binding.tabLayout
tabLayout.getTabAt(0)!!.text = "First Tab"

 

 

 

참조: https://github.com/material-components/material-components-android/issues/1162

 

 

 

구독과 공감은 블로그 운영에 큰 힘이 됩니다!
긍정적인 댓글 남겨주시면 감사드리며,
보완해야 할 점이 있으면 댓글로 남겨주셔도 좋습니다!

728x90
반응형