java.lang.NullPointerException: Missing required view with ID:

21,904

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 its bind() 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.

Share:
21,904
Alexei
Author by

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, 2022

Comments

  • Alexei
    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
    Paradoxy about 4 years
    Status fixed? Of course not. I still have this issue.
  • user158
    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
    Paradoxy about 4 years
    I 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
    user158 about 4 years
    @Paradoxy I also faced this issue, it is already reported which is not fixed.
  • mochadwi
    mochadwi about 4 years
    The error still occurred to me, when using <merge> layout for dynamically added view, e.g: parent_layout.xml -> addView for parent_layout.xml child using MyMergeLayoutBinding.inflate(inflater, viewGroup, false) -> my_merge_layout.xml is using <merge> as parent tag
  • mochadwi
    mochadwi about 4 years
    the above 3 issue also occurred to me
  • Erik
    Erik about 4 years
    Still an issue today with AS v3.6.1 and AGP v3.6.1.
  • user158
    user158 about 4 years
    @Erik earlier mentioned issues are fixed for me in the canary.
  • mochadwi
    mochadwi about 4 years
    would you mind giving us information which version of canary, that this problem is fixed? @user158
  • 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
    Udayaditya Barua about 4 years
    I got this when I had an include which was referring to a layout with just Toolbar in the layout as the root.
  • mkuech
    mkuech about 4 years
    In 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
    zgluis about 4 years
    Done. This fails: Android Studio 3.6.2 - This works: Android Studio 4.1 Canary 5
  • cking24343
    cking24343 about 4 years
    I 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
    Jenish about 4 years
    Can't believe setting same name will actually works for me.
  • Joan
    Joan almost 4 years
    Thanks! In my case i was also declaring a different binding for the included layout. Using the root layout is enough
  • mochadwi
    mochadwi almost 4 years
    this 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
    Jake Wharton over 3 years
    This is not required as of AGP 4.0 or newer.
  • paul_f
    paul_f over 3 years
    Still happening for me in 4.0.1 (Main Rlease)
  • Volodymyr R.tmnko
    Volodymyr R.tmnko over 3 years
    Adding the id to the </include/> tag fixed the issue. AndroidStudio 4.0 - 4.1
  • P1NG2WIN
    P1NG2WIN over 3 years
    Thanks a lot! Problem was in same name of two layout in differen feature modules
  • NizarETH
    NizarETH about 3 years
    you can't get child view without id
  • Ehma Africa
    Ehma Africa almost 3 years
    Best answer for my multiple module situation. Thanks man
  • Narayan soni
    Narayan soni over 2 years
    any other way to add id on tabitem with viewBinding enabled
  • j2emanue
    j2emanue over 2 years
    great 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
    Elyes Mansour over 2 years
    Thanks ! This was my problem as well. It happened even though one of the modules wasn't even being used by the app.
  • Sjd
    Sjd over 2 years
    ok great! but why? is there an explanation?
  • Yasser AKBBACH
    Yasser AKBBACH over 2 years
    You saved my a$$ thank you
  • S. Alawadi
    S. Alawadi over 2 years
    Even if there is no explanation, but thank you to the moon
  • Chee-Yi
    Chee-Yi about 2 years
    The reason is explained in stackoverflow.com/a/66325932/5020627.
  • manoellribeiro
    manoellribeiro about 2 years
    Thanks, 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
    Mahmoud Omara almost 2 years
    this answer should be the accepted answer for this issue
  • Amr
    Amr almost 2 years
    solution: Don't give id to your included layout.
  • qkx
    qkx almost 2 years
    thanks 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.