java.lang.NullPointerException: Missing required view with ID:
Solution 1
I encountered this issue but in my case the issue is the include
flag. The workaround I found is to make the view id to be the same as the id of the root view of the included layout.
activity_layout.xml
<LinearLayout>
<include android:id="@+id/widget1" layout="@layout/my_widget" />
</LinearLayout>
my_widget.xml
<LinearLayout
android:id="@+id/widget1">
</LinearLayout>
Solution 2
If someone is getting this with TabLayout
and ViewBinding
enabled then you might have set id for the TabItem
. Removing id from TabItem
rseolved the issue for me.
Solution 3
Please check, maybe you have the same layout files in different modules.
Solution 4
Watch out, if you use <merge>
inside your included layout XML, the logic is different, as pointed out here:
Exploring Android View Binding in Depth
If we try to give this an ID, the view binding won’t generate the ID in the binding class so we can’t access the view as we did in the case of the normal include.
In this case, we have PlaceholderBinding which is an auto-generated class for placeholder.xml (our<merge>
layout file). We have to call itsbind()
method and pass the root view of the layout in which we included it.
<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
}
Solution 5
Update
This should be fixed in latest Beta, Carnary versions of Android Studio
Still there is an issue when using View Binding with material tab layout tab items which is reported here and have not fixed yet.
This is bug in ViewBinding which is reported in the issue tracker in following places.
Alexei
I'm working as Java (Middle) developer (5 years). But I also has experience on Android development by Kotlin (3 years). Also I like Emacs.
Updated on July 09, 2022Comments
-
Alexei almost 2 years
Android Studio 3.6
in app/build.gradle:
android { viewBinding.enabled = true
Here my 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" />
and another xml the 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" />
here my activity:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater) setContentView(binding.root) }
the app is build and run. Nice.
Now I move id -
android:id="@+id/bluetoothBottonMainContainer"
to outer container like this:
<?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" />
app is build, but when run I get runtime error in this line:
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
error:
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
-
Paradoxy about 4 yearsStatus fixed? Of course not. I still have this issue.
-
user158 about 4 years@Paradoxy it should works now, upgrade your Android studio to latest version, since viewbinding is coming from build tools in Android Studio
-
Paradoxy about 4 yearsI am using android studio 3.6 from 12/2/2020 and yet the problem didn't go away for some of my activities. I have a linearLayout, which has tabLayout, viewPager, etc as its childs. My tabLayout itself has 3 tab items. I get Missing required view with ID for tab items. When I remove their id, this error goes away. I am using simple ids such as @id/tabitem, etc. This activity also has a drawerlayout with navigation. By using viewbinding, its toggle disappears even though I call .setDisplayHomeAsUpEnabled(true).In the end I added tools:viewBindingIgnore="true" and used that good old findviewbyid.
-
user158 about 4 years@Paradoxy I also faced this issue, it is already reported which is not fixed.
-
mochadwi about 4 yearsThe error still occurred to me, when using
<merge>
layout for dynamically added view, e.g:parent_layout.xml
-> addView forparent_layout.xml
child usingMyMergeLayoutBinding.inflate(inflater, viewGroup, false)
->my_merge_layout.xml
is using<merge>
as parent tag -
mochadwi about 4 yearsthe above 3 issue also occurred to me
-
Erik about 4 yearsStill an issue today with AS v3.6.1 and AGP v3.6.1.
-
user158 about 4 years@Erik earlier mentioned issues are fixed for me in the canary.
-
mochadwi about 4 yearswould you mind giving us information which version of canary, that this problem is fixed? @user158
-
user158 about 4 years@mochadwi I used AS 4.0 canary 9, should be also fixed on latest versions like 4.1 canary 1
-
Udayaditya Barua about 4 yearsI got this when I had an include which was referring to a layout with just Toolbar in the layout as the root.
-
mkuech about 4 yearsIn my experience, this will also happen when you try to mix simple ViewBinding with DataBinding layouts (if only some of your layouts involved have the
layout
root). -
zgluis about 4 yearsDone. This fails: Android Studio 3.6.2 - This works: Android Studio 4.1 Canary 5
-
cking24343 about 4 yearsI agree, this is dumb. My solution was to keep id for include tag and remove id of top level of the constraintLayout in the layout reference for include tag.
-
Jenish about 4 yearsCan't believe setting same name will actually works for me.
-
Joan almost 4 yearsThanks! In my case i was also declaring a different binding for the included layout. Using the root layout is enough
-
mochadwi almost 4 yearsthis also works for me. turns out the id inside included layout not found if we configure id on both
<include>
tag or the parent layout used -
Jake Wharton over 3 yearsThis is not required as of AGP 4.0 or newer.
-
paul_f over 3 yearsStill happening for me in 4.0.1 (Main Rlease)
-
Volodymyr R.tmnko over 3 yearsAdding the id to the </include/> tag fixed the issue. AndroidStudio 4.0 - 4.1
-
P1NG2WIN over 3 yearsThanks a lot! Problem was in same name of two layout in differen feature modules
-
NizarETH about 3 yearsyou can't get child view without id
-
Ehma Africa almost 3 yearsBest answer for my multiple module situation. Thanks man
-
Narayan soni over 2 yearsany other way to add id on tabitem with viewBinding enabled
-
j2emanue over 2 yearsgreat answer. life saver. in my case it was a custom view and i could not retrieve it as the include layout had a merge tag.
-
Elyes Mansour over 2 yearsThanks ! This was my problem as well. It happened even though one of the modules wasn't even being used by the app.
-
Sjd over 2 yearsok great! but why? is there an explanation?
-
Yasser AKBBACH over 2 yearsYou saved my a$$ thank you
-
S. Alawadi over 2 yearsEven if there is no explanation, but thank you to the moon
-
Chee-Yi about 2 yearsThe reason is explained in stackoverflow.com/a/66325932/5020627.
-
manoellribeiro about 2 yearsThanks, it happened when I migrated a lib used by my project to view binding and it had a layout with same name on my project.
-
Mahmoud Omara almost 2 yearsthis answer should be the accepted answer for this issue
-
Amr almost 2 yearssolution: Don't give id to your included layout.
-
qkx almost 2 yearsthanks man! You saved me a lot of time, wasted an hour with this...Sadly it is exactly as I thought, it is just ANOTHER maddening undocumented bug in Android API.