java.lang.NullPointerException : ID가있는 필수보기 누락 :

22
a_subscriber 2019-10-25 08:18.

안드로이드 스튜디오 3.6

app / build.gradle에서 :

android {
viewBinding.enabled = true

여기 내 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <View
            android:id="@+id/viewPointNotSelect"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:background="@drawable/circle_transparent"
            app:layout_constraintBottom_toBottomOf="@+id/separator"
            app:layout_constraintEnd_toStartOf="@+id/separator"
app:layout_constraintTop_toTopOf="parent" />

그리고 다른 xml은 unclude prev. xml :

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

        <include
            android:id="@+id/qrBottonContainer"
            layout="@layout/qr_bottom_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

여기 내 활동 :

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
}

앱이 빌드되고 실행됩니다. 좋은.

이제 이드 이동- android:id="@+id/bluetoothBottonMainContainer"

다음과 같이 외부 컨테이너에 :

<?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"
    android:id="@+id/bluetoothBottonMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="104dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <View
            android:id="@+id/viewPointNotSelect"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:background="@drawable/circle_transparent"
            app:layout_constraintBottom_toBottomOf="@+id/separator"
            app:layout_constraintEnd_toStartOf="@+id/separator"
            app:layout_constraintTop_toTopOf="parent" />

앱이 빌드되었지만 실행할 때 다음 줄에 런타임 오류가 발생합니다.

binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)

오류:

10-25 11:11:51.290 E/AndroidRuntime(14128): FATAL EXCEPTION: main
10-25 11:11:51.290 E/AndroidRuntime(14128): Process: com.myproject.debug, PID: 14128
10-25 11:11:51.290 E/AndroidRuntime(14128): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.debug/com.myproject.ui.actviity.QRBluetoothSwipeActivity}: java.lang.NullPointerException: Missing required view with ID: bluetoothBottonMainContainer
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 10-25 11:11:51.290 E/AndroidRuntime(14128): at android.os.Handler.dispatchMessage(Handler.java:102) 10-25 11:11:51.290 E/AndroidRuntime(14128): at android.os.Looper.loop(Looper.java:148) 10-25 11:11:51.290 E/AndroidRuntime(14128): at android.app.ActivityThread.main(ActivityThread.java:5417) 10-25 11:11:51.290 E/AndroidRuntime(14128): at java.lang.reflect.Method.invoke(Native Method) 10-25 11:11:51.290 E/AndroidRuntime(14128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-25 11:11:51.290 E/AndroidRuntime(14128): Caused by: java.lang.NullPointerException: Missing required view with ID: bluetoothBottonMainContainer
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.myproject.databinding.BluetoothBottomContainerBinding.bind(BluetoothBottomContainerBinding.java:114)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.myproject.databinding.QrBluetoothSwipeActivityBinding.bind(QrBluetoothSwipeActivityBinding.java:76)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.myproject.databinding.QrBluetoothSwipeActivityBinding.inflate(QrBluetoothSwipeActivityBinding.java:62)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.myproject.databinding.QrBluetoothSwipeActivityBinding.inflate(QrBluetoothSwipeActivityBinding.java:52)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at com.myproject.ui.actviity.QRBluetoothSwipeActivity.onCreate(QRBluetoothSwipeActivity.kt:31)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.Activity.performCreate(Activity.java:6251)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-25 11:11:51.290 E/AndroidRuntime(14128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-25 11:11:51.290 E/AndroidRuntime(14128):     ... 9 more
10-25 11:11:51.291 W/ActivityManager(  780):   Force finishing activity com.myproject.debug/com.myproject.ui.actviity.QRBluetoothSwipeActivity
10-25 11:11:51.307 I/Icing   (11529): Indexing done com.google.android.gms-apps

13 answers

32
Arst 2020-03-15 13:19.

이 문제가 발생했지만 제 경우에는 문제가 include플래그입니다. 내가 찾은 해결 방법은 뷰 ID를 포함 된 레이아웃의 루트 뷰 ID와 동일하게 만드는 것입니다.

activity_layout.xml

<LinearLayout>
    <include android:id="@+id/widget1" layout="@layout/my_widget" />
</LinearLayout>

my_wideget.xml

<LinearLayout
    android:id="@+id/widget1">
</LinearLayout>
13
user158 2019-11-01 19:27.

최신 정보

이 문제는 Android Studio의 최신 베타, Carnary 버전에서 수정되어야합니다.

여기 에보고 되고 아직 수정되지 않은 재질 탭 레이아웃 탭 항목과 함께보기 바인딩을 사용할 때 여전히 문제가 있습니다 .


이것은 ViewBinding의 버그로 다음 위치의 이슈 트래커에보고됩니다.

  • https://issuetracker.google.com/issues/143729912
  • https://issuetracker.google.com/issues/143683987
3
adi 2020-04-17 19:49.

제 경우 에는 포함 된 레이아웃 내에서 ID를 제거 했는데 제대로 작동합니다!

main.xml

    <RelativeLayout>   
    ....
    <include
        include="@layout/included_layout"   
        android:id="@+id/view_included_layout"/>
    </RelativeLayout>

included_layout.xml

`   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/top_level_layout">  <!-- Remove this id -->    
        ....
    </LinearLayout>
`

내 Android Studio 버전은 3.6.2이고 build.gradle 버전은 3.6.1입니다.

2
David Arribas 2020-06-25 17:14.

해결책은 기본 컨테이너의 ID를 제거하는 것입니다. 그리고 부모 안에 다른 용기에 넣으십시오.

1.- 레이아웃

    <include
    android:id="@+id/example"
    layout="@layout/account_current_payment_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    

2.- 레이아웃 포함

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/example2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2
paul_f 2020-08-11 09:39.

recyclerView의 뷰에 셀을 잘못 바인딩 한 경우에도이 오류가 발생할 수 있습니다. 보기를 잘못된 레이아웃에 바인딩하면 예상되는 하위보기를 찾을 수 없다는 오류가 표시됩니다.

이로 인해 오랫동안 당황했습니다.

해결책은 뷰에 바인딩하는 레이아웃이 ViewBinding과 동일한 레이아웃인지 확인하는 것입니다.

예를 들어 다음을 확인하십시오.

R.layout.my_new_view

다음에 바인딩됩니다.

MyNewViewBinding

또는이 오류가 발생합니다.

1
ZX C 2020-03-06 13:53.

동일한 버그를 만났습니다. "외부 컨테이너"에 래퍼 뷰를 추가하여이 버그를 방지 할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bluetoothBottonMainContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="104dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <View
            android:id="@+id/viewPointNotSelect"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:background="@drawable/circle_transparent"
            app:layout_constraintBottom_toBottomOf="@+id/separator"
            app:layout_constraintEnd_toStartOf="@+id/separator"
            app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

1
Maxim Firsoff 2020-03-17 14:27.

다른 모듈에 동일한 레이아웃 파일이 있는지 확인하십시오.

1
juliusspencer 2020-05-16 11:13.

내 솔루션은 비슷했지만 약간 다르며 대화 상자에 포함되었습니다.

<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:minHeight="@dimen/complete_programme_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
    android:id="@+id/include" <!-- Removing this line worked -->
    layout="@layout/background_triangle_medium_violet_gradient" />
1
Starwave 2020-06-16 18:28.

나에게 문제는 여전히 Android 3.x 버전을 사용하고 있지만 새로운 Gradle 4.0.0 버전을 사용하고 있다는 것입니다.

"정보-> 업데이트 확인"을 누르고 Android Studio를 업데이트했으며 오류는 더 이상 없습니다.

1
rahat 2020-12-13 01:14.

제 경우에는

<merge>

다른 레이아웃에 포함 된 레이아웃의 태그. 방금 LinearLayout으로 대체했습니다.

0
Ananthu Subramanian 2020-05-18 14:58.

getRootView ()는 포함 된 레이아웃을 반환하고 변경할 수 있습니다.

0
Big_Chair 2020-11-16 20:24.

<merge>포함 된 레이아웃 XML 내에서 사용하는 경우 여기에 설명 된대로 로직이 다릅니다.
Android 뷰 바인딩을 자세히 살펴보기

여기에 ID를 부여하려고하면 뷰 바인딩이 바인딩 클래스에서 ID를 생성하지 않으므로 일반 포함의 경우처럼 뷰에 액세스 할 수 없습니다.
이 경우 placeholder.xml ( 레이아웃 파일)에 대해 자동 생성 된 클래스 인 PlaceholderBinding 이 있습니다. 우리는 그것의 메서드 를 호출하고 우리가 포함 된 레이아웃의 루트 뷰를 전달해야합니다.<merge>bind()

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/placeholder" />

</androidx.constraintlayout.widget.ConstraintLayout>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentOrderBinding.inflate(layoutInflater, container, false)
        placeholderBinding = PlaceholderBinding.bind(binding.root)
        placeholderBinding.tvPlaceholder.text = getString(R.string.please_wait)
        return binding.root
    }
0
MarkT 2020-12-18 02:59.

이것은 지금 나에게 일어나고 있습니다. 내 xml 파일에서 다음과 같이 호출 할 수 있기 때문에 이런 일이 발생했음을 알았습니다. Activity_main.xml / Activity_other.xml 동일한 ID (+ id / bttNext)를 가진 위젯 (이 경우 버튼)이있었습니다.

(즉, activity_main.xml과 Activity_other는 동일한 + id를 가진 버튼을 가졌던 것을 의미합니다)

기기에서 내 앱을 테스트하자마자 :

java.lang.NullPointerException : ID가있는 필수보기 누락 :

등장

두 번째 활동 버튼의 + id를 변경하자마자 (bttNext1로 이름이 변경되었다고 말할 수 있습니다) aspp가 잘 작동했습니다.

요약하자면 : 버튼 (기타 위젯)에서 동일한 ID를 사용하지 마십시오. 별도의 활동에 있는지 여부는 중요하지 않습니다.

Related questions

MORE COOL STUFF

Jana Duggar: 사랑에 대해 그녀가 말한 모든 것과 '하나'를 찾기 위한 5년의 창

Jana Duggar: 사랑에 대해 그녀가 말한 모든 것과 '하나'를 찾기 위한 5년의 창

Jana Duggar는 사랑에 대한 그녀의 검색에 대해 공개했습니다. 다음은 그녀가 결혼을 위한 5년 창구와 주제에 대해 말한 모든 것입니다.

Sam Heughan은 자신이 '아웃랜더' 세트에서 무엇을 취할지 정확히 알고 있습니다.

Sam Heughan은 자신이 '아웃랜더' 세트에서 무엇을 취할지 정확히 알고 있습니다.

'아웃랜더'의 스타 샘 흑한은 최근 자신을 스타로 만든 역할인 제이미 프레이저를 기억하기 위해 세트장에서 무엇을 할 것인지를 밝혔다.

Fab Four 내부자에 따르면 밴드의 '가장 신나는 공연' 중 하나였던 초기 비틀즈 노래

Fab Four 내부자에 따르면 밴드의 '가장 신나는 공연' 중 하나였던 초기 비틀즈 노래

'She Loves You'는 한 시간 만에 작곡되고 하루 만에 녹음되었으며, 비틀즈의 경력 중 최고의 공연 중 하나인 비틀즈의 노래였습니다.

Dolly Parton은 다른 손자들이 아무도 하지 않을 때 그녀의 '병약한' 할머니를 도왔습니다. 그녀는 또한 그녀에게 장난을 쳤습니다

Dolly Parton은 다른 손자들이 아무도 하지 않을 때 그녀의 '병약한' 할머니를 도왔습니다. 그녀는 또한 그녀에게 장난을 쳤습니다

Dolly Parton과 그녀의 할머니 Bessie는 특별한 관계를 가졌습니다. Parton 할머니는 매우 아팠지만 Dolly는 그녀에게 장난을 쳤습니다.

타누키가 어떻게 실제 존재이면서 신화 속 존재일 수 있을까?

타누키가 어떻게 실제 존재이면서 신화 속 존재일 수 있을까?

통통한 배, 장난기 어린 눈, 일본 정원에 돌 조각상으로 나타나는 습관을 가진 타누키는 신화와 현실의 경계를 모호하게 만듭니다.

세상에서 가장 단단한 나무는 무엇인가?

세상에서 가장 단단한 나무는 무엇인가?

참나무나 히코리는 튼튼하다고 생각할 수도 있지만, 세상에서 가장 단단한 나무에 비하면 그 근처에도 못 미칩니다.

바다 깊은 곳에 숨어 있는 가장 무서운 바다 생물 10종

바다 깊은 곳에 숨어 있는 가장 무서운 바다 생물 10종

바다는 아름다움으로 가득하지만, 지구상에서 가장 무서운 바다 생물들도 숨어 있습니다. 이 동물들 중 다수는 해수면 아래, 어둡고 압력이 높은 심해에 숨어 있습니다.

마인크래프트 최고의 검 마법 부여 10가지

마인크래프트 최고의 검 마법 부여 10가지

적대적인 몹과 싸우거나 PvP 시나리오에 대비하고 있다면, 마인크래프트에서 제공하는 최고의 검 마법 부여를 아는 것이 큰 도움이 될 수 있습니다. 검에 마법을 부여하면 더 큰 피해를 입히고, 몹 드랍률을 높이고, 검의 내구도를 연장할 수 있습니다.

곰 요약: 마커스는 시카고에서 가장 친절하고, 부드럽고, 성실한 사람인가요?

곰 요약: 마커스는 시카고에서 가장 친절하고, 부드럽고, 성실한 사람인가요?

“Children”은 유명인의 카메오와 충격적인 소식을 전합니다.

9,800달러로 이 7인승 2008 Toyota RAV4를 타고 싶으신가요?

9,800달러로 이 7인승 2008 Toyota RAV4를 타고 싶으신가요?

이 RAV4는 상태가 매우 양호하며 의자 게임에 적합하다고 주장됩니다.

휴스턴의 여름 더위 속에서 Amazon 배달 밴이 불타 폭발

휴스턴의 여름 더위 속에서 Amazon 배달 밴이 불타 폭발

드론 영상에는 화재를 진압하기 위해 애쓰는 소방관들의 모습이 포착됐다.

Marvel의 Black Panther 쇼는 지금까지 가장 중요한 애니메이션 쇼가 될 것입니다

Marvel의 Black Panther 쇼는 지금까지 가장 중요한 애니메이션 쇼가 될 것입니다

Eyes of Wakanda는 MCU에 직접 연결되며 X-Men '97, What If..., Daredevil 등에 대한 업데이트도 제공됩니다.

안나 윈투어, 37년간의 보그 편집장직에서 물러나다 - 그리고 그녀는 사람을 채용합니다!

안나 윈투어, 37년간의 보그 편집장직에서 물러나다 - 그리고 그녀는 사람을 채용합니다!

1988년부터 미국판 보그에 합류한 오랜 편집장은 6월 26일 목요일 아침 직원 회의에서 이 소식을 발표했습니다.

케이트 미들턴의 아름다운 사파이어 약혼 반지에 대한 모든 것 (그리고 반지에서 영감을 얻은 왕실 전래품!)

케이트 미들턴의 아름다운 사파이어 약혼 반지에 대한 모든 것 (그리고 반지에서 영감을 얻은 왕실 전래품!)

케이트 미들턴의 약혼 반지는 윌리엄 왕자가 프로포즈하기 전에 다이애나 공주의 소유였던 것으로 유명합니다. 이 역사적인 작품에 대한 모든 정보를 여기에서 확인하세요.

라구나 비치 출연진: 그들은 지금 어디에 있나요?

라구나 비치 출연진: 그들은 지금 어디에 있나요?

'라구나 비치'는 2000년대 중반 고등학생 Lauren Conrad, Kristin Cavallari, Stephen Colletti 등의 삶을 따라갔습니다. MTV 리얼리티 시리즈의 출연진이 현재 있는 곳은 다음과 같습니다.

Dog the Bounty Hunter의 아내는 누구인가? Francie Frane에 대한 모든 것

Dog the Bounty Hunter의 아내는 누구인가? Francie Frane에 대한 모든 것

전직 목장주이자 현상금 사냥꾼인 도그와 결혼한 프랜시 프랜에 대한 모든 정보를 소개합니다.

대명사는 정말 무섭다

너무 자주 그렇듯 갑자기 와닿는다 다시 중간쯤이야 <아니, 태평양에 있는 사람 말고 마룬, 카니발에 있는 사람> 모든 헤드라인이 시끄러워…..

작문 실력을 향상시키는 3가지 입증된 방법

글쓰기의 이점과 그렇게 하는 데 도움이 되는 전략

작문 실력을 향상시키는 3가지 입증된 방법

2022년에는 GPT-3가 출시되면서 글쓰기 능력이 덜 중요해진 것 같습니다. AI를 사용하면 글이 서투른 사람들도 양질의 직원을 생성할 수 있습니다.

느린 침식: 사람들이 경력을 낭비하는 방식 공개

느린 침식: 사람들이 경력을 낭비하는 방식 공개

오늘날과 같이 급변하고 경쟁이 치열한 세상에서 경력은 개인의 성장, 재정적 안정 및 전반적인 삶의 만족도에 중요한 역할을 합니다. 그러나 많은 사람들은 침체와 불만족의 끊임없는 순환에 휩싸여 직업 생활에서 점차 쇠퇴하고 있습니다.

팟캐스트가 영향력 있는 이유는 무엇입니까?

팟캐스트가 영향력 있는 이유는 무엇입니까?

지난 주에 저는 PressGazette(영국 언론 뉴스 웹사이트)를 통해 발송된 보도 자료를 보았습니다. 이 기사는 Scandi 팟캐스트 호스팅 및 광고 회사인 Acast가 팟캐스팅에서 "아마도 가장 영향력 있는" 게시자 컨소시엄을 이끌 것이라고 발표했습니다.

Language