[android] java.lang.NullPointerException : ID가있는 필수보기가 누락되었습니다.
안드로이드 스튜디오 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은 제외 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)
}
앱이 빌드되고 실행됩니다. 좋은.
이제 나는 id를 움직인다- 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
답변
이 문제가 발생했지만 내 경우 문제는 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>
답변
최신 정보
이것은 최신 베타, Carnary 버전의 Android Studio에서 수정되어야합니다.
여기 에보고 되었지만 아직 수정되지 않은 재료 탭 레이아웃 탭 항목으로 뷰 바인딩을 사용할 때 여전히 문제가 있습니다 .
이것은 다음 위치의 이슈 트래커에서보고되는 ViewBinding의 버그입니다.
답변
이 버그를 피하기 위해 “외부 컨테이너”에 래퍼 뷰를 추가 할 수 있습니다.
<?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>
답변
다른 모듈에 동일한 레이아웃 파일이 있는지 확인하십시오.
답변
내 경우 에는 포함 된 레이아웃 내부에서 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입니다.