91

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
3
  • 4
    Renaming the id of the view which was producing a NPE worked for me. Went from drawerLayout to drawerLayout_main and this resolved my issue.
    – 11m0
    Commented Dec 24, 2019 at 23:00
  • 12
    Hi while working with merge tags you need to extra careful, I have written a blog post completely explaining view binding. and why merge tag doesnt work so easily, checkout 👉Androidbites|ViewBinding Commented Dec 6, 2020 at 10:55
  • @11m0 please create an answer so that I can upvote it. This is the ONLY answer which worked for us!
    – MidasLefko
    Commented Feb 21, 2021 at 15:22

33 Answers 33

99

If someone is getting this with TabLayout and ViewBinding enabled then you might have set id for the TabItem. Removing id from TabItem resolved the issue for me.

Update: For those people wondering how they can switch tabs without having an id and a ViewPager2, here is the documentation that recommends using TabLayoutMediator:

Create swipe views with tabs using ViewPager2

2
79

Please check, maybe you have the same layout files in different modules.

8
  • 7
    Thanks a lot! Problem was in same name of two layout in differen feature modules
    – P1NG2WIN
    Commented Dec 12, 2020 at 23:01
  • 3
    Best answer for my multiple module situation. Thanks man Commented Jun 4, 2021 at 7:38
  • 2
    Thanks ! This was my problem as well. It happened even though one of the modules wasn't even being used by the app. Commented Oct 28, 2021 at 12:23
  • 1
    this is what happened to me! I have a xml with the same name in app module, it replaces my xml in another module and causes NPE
    – Jeff
    Commented Aug 10, 2022 at 9:32
  • 1
    that's right very helpful Commented Jan 2, 2023 at 15:14
53

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
    }
5
  • 4
    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.
    – j2emanue
    Commented Oct 28, 2021 at 7:38
  • 1
    this answer should be the accepted answer for this issue Commented Apr 27, 2022 at 15:37
  • solution: Don't give id to your included layout.
    – Amr
    Commented May 24, 2022 at 15:43
  • 1
    How to align views around included view then? In constraint layout Commented Dec 16, 2022 at 5:03
  • 1
    Thanks, this was my problem with the merge. Commented Jun 22, 2023 at 9:16
52

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>
5
  • 3
    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.
    – cking24343
    Commented Apr 14, 2020 at 16:44
  • 3
    Can't believe setting same name will actually works for me.
    – Jenish
    Commented Apr 18, 2020 at 6:12
  • Thanks! In my case i was also declaring a different binding for the included layout. Using the root layout is enough
    – Joan
    Commented May 15, 2020 at 8:27
  • 1
    This is not required as of AGP 4.0 or newer. Commented Jul 27, 2020 at 12:59
  • Adding the id to the </include/> tag fixed the issue. AndroidStudio 4.0 - 4.1 Commented Oct 28, 2020 at 16:06
22

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.

16
  • 27
    Status fixed? Of course not. I still have this issue.
    – Paradoxy
    Commented Feb 25, 2020 at 14:28
  • 3
    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.
    – Paradoxy
    Commented Feb 26, 2020 at 13:46
  • 4
    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
    Commented Mar 3, 2020 at 3:09
  • 2
    Still an issue today with AS v3.6.1 and AGP v3.6.1.
    – Erik
    Commented Mar 3, 2020 at 14:30
  • 3
    Still happening for me in 4.0.1 (Main Rlease)
    – paul_f
    Commented Jul 31, 2020 at 16:10
10

You can also get this error if you incorrectly bind a cell to a view in a recyclerView. If you bind a view to the wrong layout, then you will get an error stating that it cannot find the expect child views.

This had me stumped for a long time.

The solution is to ensure the layout you are binding to the view is the same layout as the ViewBinding.

For example, makesure:

R.layout.my_new_view

is bound to:

MyNewViewBinding

Or you will get this error.

10

You should delete the TabItem id and use the TabLayout API to get the item, this workaround works if you are using Material Design

check this: Github answer

4

In my case, there was another file with the same name in another module.

I had item_view.xml in module A and item_view.xml in module B, so databinding couldn’t distinguish between them.

I changed the name of one of them and it fixed.

1
  • same for me; my problem was also with data binding. I copied and pasted the layout code because I had two similar layouts, but I forgot to associate the second layout with its viewmodel. It remained associated with the other viewmodel and the variables had different names, so the binding crashed, showing "Missing required view with ID:" Commented Nov 15, 2022 at 12:24
3

The solution is remove the id of the main container. And put in other container inside the parent.

1.- Layout

    <include
    android:id="@+id/example"
    layout="@layout/account_current_payment_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    

2.- Include Layout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/example2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3

This is happening to me right now. I just noticed that this happened because in my xml files lets call them: Activity_main.xml / Activity_other.xml I used to have a widget (a button in this case) with the same id, (+id/bttNext)

(that means activity_main.xml and Activity_other used to have a button with the same +id)

as soon I test my app on a device the:

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

appeared

as soon I changed the second activity´s button´s +id (let's say renamed bttNext1) the app worked fine

To summarize: avoid using the same id in buttons (et al widgets) does not matter if they´re in separate activities

2

In my case , I removed the id inside the included layout and it works properly !

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>
`

My Android Studio version is 3.6.2 and version of build.gradle is 3.6.1

1
  • 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
    – mochadwi
    Commented Jun 13, 2020 at 12:24
2

Cleaning and Rebuilding project helped me

1
  • Same here. I couldn't find the missing ID in the entire project. So, Android Studio might have built with some internal cache that was messed up. I cleaned and rebuild and there is no crash
    – Andrew
    Commented May 30 at 13:11
2

I tried many ways, but all didn't work.

Finally, the problem is there is an XML with the same name in another module

1

I meet the same bug.You can add a wrapper view in your "outer container" to avoid this 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>

0
1

For me, the issue was that I was still using Android 3.x version, but was using the new Gradle 4.0.0 version.

Pressed "About->Check for Updates", updated Android Studio, and the error is no more.

1

just remove .gradle directory and invlidate caches/restart the project, keep backup of project. I was facing the same issue, solved using this method.

1

This is error can also happen if you by mistake use an xml layout for the fragment but use a different one for the binding.I was doing when I encountered the error

class LoginFragment :Fragment(R.layout.login_fragment) { 
private val binding by viewBinding(LoginFragmentNewBinding::bind)//here I am referring to a new xml file called login_fragment_new which is different from the one in the Fragment's constructor (i.e login_fragment)
}

and using the same xml file fixed it for me.

1

Got this issue with custom view , where is wrongly took this instead of super

constructor(context: Context, attrs: AttributeSet?) : this(context,attrs) { //<-Change this to super

}
1

I was getting error because in my case main app and module has same layout (layout_calling_waiting) with different views which was not able to find in app's layout. So the error waas giving.

1

i faced same problem and i solved it by remove this line in the layout.

tools:context=".ui.SplashScreenActivity
1
  • 1
    tools:context doesn't matter when an app runs, maybe it was indirectly because after removing this you triggered builds/cleans that needed to happen.
    – David
    Commented Mar 7, 2023 at 12:01
1

In my case this problem was related with same naming of xml files in different modules.

Why got problem:

I had 2 xml files with the same name (item_tag.xml) in different modules (app, landing). Compiler doesn't know which one need to use, and I got an error during runtime (every time when I opened a screen with this binding).

Solution:

I renamed xml file, and one become item_landing_tag.xml (and got generated ItemLandingTagBinding class instead of ItemTagBinding).

Hope it helps someone :)

1
  • 1
    I faced this problem too. Had a common dialog which was in 'app' then i moved to 'ui' . Instead of moving the layout files, i copy pasted them and caused this crash because now it existed in 2 modules. Commented Feb 16, 2023 at 11:22
1

I had the same problem when I tried to create ViewBinding from the existing view using bind() function with invalid layout id.

So, keep in mind that MyLayoutBinding.bind(view) should get view inflated from R.layout.my_layout (not R.layout.my_another_layout and so on)

/** DON'T: call bind() with view inflated from another layout id */
val view = LayoutInflater.from(context).inflate(R.layout.my_invalid_layout) // <- be careful here
val binding = MyCorrectLayoutBinding.bind(view)
// crash

/** DO: call bind() with view inflated from the same layout id */
val view = LayoutInflater.from(context).inflate(R.layout.my_correct_layout)
val binding = MyCorrectLayoutBinding.bind(view)
// ok
0

getRootView() returns the included layout and can make changes in it

0

It may be late but. For everyone who come across this issue. I first tried out everything I saw here with no luck. I found out that using snake case ids fixes the problem. I had an id as tabContainer annd it was causing the NPE. Renaming it to tab_container fixed the bug for me.

0

Same problem but just in custom view activity I put the correct attributes.

In kotlin:

class CanvasNew @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null):View(context,attrs){

0

For my base my_main_layout.xml was -

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

  <ScrollView style="@style/ScrollViewContainerStyle">

    <include
        android:id="@+id/include_1"
        layout="@layout/layout"></include>
    
    <include
        android:id="@+id/include_1"
        layout="@layout/layout"></include>
    
    <include
        android:id="@+id/include_1"
        layout="@layout/layout"></include>
    </include></layout>

 </ScrollView>

And my included layout.xml was -

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/tv_title"
    tools:text="This is a tile" />
</LinearLayout>

I just solved my problem by wrapping it with <layout> -

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="visible">
<TextView
    android:id="@+id/tv_title"
    tools:text="This is a tile" />
</LinearLayout>
</layout>
0

In my case I was using incorrect FragmentBinding class in onCreateView method to inflate layout:

open class InfoFragment : BaseFragment() {

    private val binding by viewBinding(FragmentInfoBinding::bind)
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        // NOTE: using incorrect binding class
        return FragmentEducationBinding.inflate(inflater, container, false).root
    }
    ...
}

To solve this issue I should have to use the correct binding class in onCreateView method, FragmentInfoBinding in my case:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    return FragmentInfoBinding.inflate(inflater, container, false).root
}
0

A solution could be:

init {
    id = context.obtainStyledAttributes(attrs, intArrayOf(android.R.attr.id)).let { typedArray ->
        val value = typedArray.getResourceId(0, View.NO_ID)
        typedArray.recycle()
        value
    }
    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    binding = AnyBinding.inflate(inflater, this)
}
0

enter image description here

Run your code with opposite { minifyEnabled }

If so now:

debug {
      minifyEnabled false
}

Then run so:

debug {
      minifyEnabled true
}

Or vice versa.

0

My solution was similar but slightly different, I had an include in my dialog

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:minHeight="@dimen/complete_programme_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
    android:id="@+id/include" <!-- Removing this line worked -->
    layout="@layout/background_triangle_medium_violet_gradient" />
1
  • 1
    you can't get child view without id
    – NizarETH
    Commented Jan 29, 2021 at 11:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.