java.lang.NullPointerException: Missing required view with ID:
Asked Answered
V

33

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
Virgate answered 25/10, 2019 at 8:18 Comment(3)
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. – Adorable
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 – Dagenham
@Adorable please create an answer so that I can upvote it. This is the ONLY answer which worked for us! – Akins
S
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

Selfcontained answered 6/2, 2021 at 15:58 Comment(2)
any other way to add id on tabitem with viewBinding enabled – Unfeigned
The reason is explained in https://mcmap.net/q/242412/-missing-required-view-with-id-with-view-binding-and-navigation-component. – Maladapted
M
79

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

Mulvihill answered 17/3, 2020 at 7:27 Comment(8)
Thanks a lot! Problem was in same name of two layout in differen feature modules – Macmahon
Best answer for my multiple module situation. Thanks man – Ohare
Thanks ! This was my problem as well. It happened even though one of the modules wasn't even being used by the app. – Ypsilanti
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. – Balcke
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 – Cioffi
that's right very helpful – Tentmaker
What about a situation where you have multiple build variants? Those layout files should have the same name in this case, how do I go about it? – Plain
this is my case. thanks for help – Toussaint
H
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
    }
Hahnke answered 16/11, 2020 at 13:24 Comment(5)
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. – Aglimmer
this answer should be the accepted answer for this issue – Berlioz
solution: Don't give id to your included layout. – Pitchford
How to align views around included view then? In constraint layout – Pyles
Thanks, this was my problem with the merge. – Hexapla
S
51

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>
Superphysical answered 15/3, 2020 at 6:19 Comment(5)
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. – Verein
Can't believe setting same name will actually works for me. – Rojas
Thanks! In my case i was also declaring a different binding for the included layout. Using the root layout is enough – Blackshear
This is not required as of AGP 4.0 or newer. – Pleuro
Adding the id to the </include/> tag fixed the issue. AndroidStudio 4.0 - 4.1 – Rain
B
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.

Barfuss answered 1/11, 2019 at 12:27 Comment(16)
Status fixed? Of course not. I still have this issue. – Desiccant
@Desiccant it should works now, upgrade your Android studio to latest version, since viewbinding is coming from build tools in Android Studio – Barfuss
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. – Desiccant
@Desiccant I also faced this issue, it is already reported which is not fixed. – Barfuss
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 – Cruciate
the above 3 issue also occurred to me – Cruciate
Still an issue today with AS v3.6.1 and AGP v3.6.1. – Topless
@Topless earlier mentioned issues are fixed for me in the canary. – Barfuss
would you mind giving us information which version of canary, that this problem is fixed? @user158 – Cruciate
@Cruciate I used AS 4.0 canary 9, should be also fixed on latest versions like 4.1 canary 1 – Barfuss
I got this when I had an include which was referring to a layout with just Toolbar in the layout as the root. – Buttaro
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). – Monahon
Done. This fails: Android Studio 3.6.2 - This works: Android Studio 4.1 Canary 5 – Scarcity
Still happening for me in 4.0.1 (Main Rlease) – Gyrose
Seems like still exist in AS 2022.1.1 – Ununa
It actually worked for a while. It worked in AGP 4.2.2 for me... Then broke again in AGP 7.4.1 (I haven't tried versions in between). Now it's still broken in AGP 8.2.2 – Farrahfarrand
G
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.

Gyrose answered 11/8, 2020 at 2:39 Comment(0)
R
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

Reena answered 15/2, 2021 at 4:40 Comment(0)
K
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.

Kcal answered 11/11, 2021 at 10:51 Comment(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:" – Overplus
P
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>
Pedrick answered 25/6, 2020 at 10:14 Comment(0)
L
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

Lancaster answered 17/12, 2020 at 19:59 Comment(0)
F
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

Fullfledged answered 17/4, 2020 at 12:49 Comment(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 – Cruciate
G
2

Cleaning and Rebuilding project helped me

Goldofpleasure answered 5/11, 2022 at 17:46 Comment(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 – Disquietude
S
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

Sharlenesharline answered 17/3, 2023 at 8:52 Comment(0)
I
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>

Isolda answered 6/3, 2020 at 6:53 Comment(0)
T
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.

Tricolor answered 16/6, 2020 at 11:28 Comment(0)
R
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.

Ruelu answered 12/2, 2021 at 5:34 Comment(0)
J
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.

Jadajadd answered 5/9, 2021 at 7:23 Comment(0)
M
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

}
Melodious answered 5/11, 2021 at 11:30 Comment(0)
A
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.

Amar answered 9/9, 2022 at 16:48 Comment(0)
D
1

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

tools:context=".ui.SplashScreenActivity
Denotation answered 25/10, 2022 at 6:43 Comment(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. – Bluebird
M
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 :)

Mariellamarielle answered 21/12, 2022 at 20:42 Comment(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. – Buttaro
B
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
Bebe answered 17/9, 2023 at 17:28 Comment(0)
S
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" />
Spraggins answered 16/5, 2020 at 4:13 Comment(1)
you can't get child view without id – Merriman
B
0

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

Binns answered 18/5, 2020 at 7:58 Comment(0)
B
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.

Bade answered 4/8, 2021 at 8:21 Comment(0)
J
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){

Joslin answered 27/8, 2021 at 14:57 Comment(0)
P
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>
Pugnacious answered 10/10, 2021 at 5:8 Comment(0)
S
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
}
Spermatid answered 3/3, 2022 at 14:33 Comment(0)
G
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)
}
Gagliano answered 13/3, 2022 at 20:56 Comment(0)
G
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.

Grandiose answered 14/7, 2022 at 6:39 Comment(0)
B
0

Solved it by using: Invalidate Caches and Clean Project.
In my case I copied some widgets from one layout to another and after that I changed the ids (so no ids were repeated).
Debug build worked correctly, but when trying to run a release build the Exception was thrown.

Bethesde answered 4/11, 2022 at 13:25 Comment(0)
L
0

I had this problem too, but apparently it was exactly what the error pointed out, a null pointer, the view got "consumed" by my custom view container that only accepted first child view after infaltion and the next view with the ID pointed out by the error was removed from hierarchy so the viewbinder could not bind them.

Leucoplast answered 30/1, 2023 at 10:19 Comment(0)
S
0

I had different layouts for phones and tablets, and the view id was missing in one of the layouts, adding it solved the problem.

In src/main/res/layout-sw600dp/fragment_satellite.xml I had the view:

....
<FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/row005"
        />
....

but in src/main/res/layout/fragment_satellite.xml there wasn't a view with the same id @+id/container, so adding it to src/main/res/layout/fragment_satellite.xml solved the problem.

Spermatid answered 5/4, 2023 at 11:54 Comment(0)

© 2022 - 2025 β€” McMap. All rights reserved.