Android Studio 3.6
no app / build.gradle:
android {
viewBinding.enabled = true
Aqui meu 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" />
e outro xml o unclude anterior. 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" />
aqui minha atividade:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
o aplicativo é construído e executado. Agradável.
Agora eu movo id - android:id="@+id/bluetoothBottonMainContainer"
para contêiner externo como este:
<?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" />
O aplicativo é compilado, mas quando executado, recebo um erro de tempo de execução nesta linha:
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
erro:
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
android
nullpointerexception
android-viewbinding
a_subscriber
fonte
fonte
Respostas:
Encontrei esse problema, mas no meu caso, o problema é a
include
bandeira. A solução alternativa que encontrei é fazer com que o ID da visualização seja igual ao ID da visualização raiz do layout incluído.activity_layout.xml
my_wideget.xml
fonte
Atualizar
Isso deve ser corrigido nas versões beta mais recentes do Carnival Studio
Ainda existe um problema ao usar a opção Exibir vinculação com itens da guia de layout da guia de material que é relatado aqui e ainda não foi corrigido.
Esse é um erro no ViewBinding, que é relatado no rastreador de problemas nos seguintes locais.
fonte
<merge>
layout para exibição adicionada dinamicamente, por exemplo:parent_layout.xml
-> addView paraparent_layout.xml
filho usandoMyMergeLayoutBinding.inflate(inflater, viewGroup, false)
->my_merge_layout.xml
está usando<merge>
como tag paiEu encontro o mesmo bug. Você pode adicionar uma exibição de wrapper no seu "contêiner externo" para evitar esse bug.
fonte
Por favor, verifique, talvez você tenha os mesmos arquivos de layout em diferentes módulos.
fonte
No meu caso, removi o ID dentro do layout incluído e ele funciona corretamente!
main.xml
included_layout.xml
Minha versão do Android Studio é 3.6.2 e a versão do build.gradle é 3.6.1
fonte