Android Studio 3.6
dans app/build.gradle:
Android {
viewBinding.enabled = true
Voici mon 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" />
et un autre xml l'include 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" />
ici mon activité:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
l'application est créée et exécutée. Agréable.
Maintenant, je déplace id - Android:id="@+id/bluetoothBottonMainContainer"
au récipient extérieur comme ceci:
<?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" />
l'application est construite, mais lors de l'exécution, j'obtiens une erreur d'exécution dans cette ligne:
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
erreur:
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
Je rencontre le même bug. Vous pouvez ajouter une vue wrapper dans votre "conteneur externe" pour éviter ce bug.
<?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>
Dans mon cas, j'ai supprimé l'identifiant à l'intérieur de la mise en page incluse et cela fonctionne correctement!
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>
`
Mon Android La version Studio est 3.6.2 et la version de build.gradle est 3.6.1
J'ai rencontré ce problème mais dans mon cas, le problème est l'indicateur include
. La solution de contournement que j'ai trouvée consiste à faire en sorte que l'ID de vue soit le même que l'ID de la vue racine de la disposition incluse.
activity_layout.xml
<LinearLayout>
<include Android:id="@+id/widget1" layout="@layout/my_widget" />
</LinearLayout>
my_wideget.xml
<LinearLayout
Android:id="@+id/widget1">
</LinearLayout>
Veuillez vérifier, vous avez peut-être les mêmes fichiers de mise en page dans différents modules.