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

AndroidNullpointerexceptionAndroid Viewbinding

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

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

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

Solution 4 - Android

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

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.

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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>

Solution 10 - Android

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>

Solution 11 - Android

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.

Solution 12 - Android

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 had a button with the same +id)

as soon I test my app in a device the:

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

appeared

as soon I changed the second activity´s button´s +id (lets say renamed bttNext1) the aspp worked fine

to sumarize: avoid use the same id in buttons (et al widgets) does not matter if they´re in separate activitys

Solution 13 - Android

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

Solution 14 - Android

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" />

Solution 15 - Android

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

Solution 16 - Android

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.

Solution 17 - Android

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){

Solution 18 - Android

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.

Solution 19 - Android

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>

Solution 20 - Android

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

}

Solution 21 - Android

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.

Solution 22 - Android

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
}

Solution 23 - Android

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)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexeiView Question on Stackoverflow
Solution 1 - AndroidArstView Answer on Stackoverflow
Solution 2 - AndroidminatoView Answer on Stackoverflow
Solution 3 - AndroidMaxim FirsoffView Answer on Stackoverflow
Solution 4 - AndroidBig_ChairView Answer on Stackoverflow
Solution 5 - Androiduser158View Answer on Stackoverflow
Solution 6 - Androidpaul_fView Answer on Stackoverflow
Solution 7 - AndroidJorge AldanaView Answer on Stackoverflow
Solution 8 - AndroidadiView Answer on Stackoverflow
Solution 9 - AndroidDavidUpsView Answer on Stackoverflow
Solution 10 - AndroidZX CView Answer on Stackoverflow
Solution 11 - AndroidStarwaveView Answer on Stackoverflow
Solution 12 - AndroidMarkTView Answer on Stackoverflow
Solution 13 - Androidjayu pipaliyaView Answer on Stackoverflow
Solution 14 - AndroidjuliusspencerView Answer on Stackoverflow
Solution 15 - AndroidAnanthu SubramanianView Answer on Stackoverflow
Solution 16 - AndroidEspera AwoView Answer on Stackoverflow
Solution 17 - AndroidEdwin Paz ss. -View Answer on Stackoverflow
Solution 18 - AndroidMohammed FathiView Answer on Stackoverflow
Solution 19 - AndroidGk Mohammad EmonView Answer on Stackoverflow
Solution 20 - AndroidManoharView Answer on Stackoverflow
Solution 21 - AndroidFarshidABZView Answer on Stackoverflow
Solution 22 - AndroidSergeyView Answer on Stackoverflow
Solution 23 - AndroidLeonardo AlapizcoView Answer on Stackoverflow