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
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>
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.
Please check, maybe you have the same layout files in different modules.
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
}
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.
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.
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
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>
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
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>
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.
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
just remove.gradle directory and invlidate caches/restart the project, keep backup of project. I was facing the same issue, solved using this method.
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.
i faced same problem and i solved it by remove this line in the layout.
tools:context=".ui.SplashScreenActivity
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" />
getRootView() returns the included layout and can make changes in it
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.
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){
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>
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
}
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.
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
}
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)
}
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.
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.
Cleaning and Rebuilding project helped me
In my case this problem was related with same naming of xml files in different modules .
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
).
I renamed xml file, and one become item_landing_tag.xml
(and got generated ItemLandingTagBinding
class instead of ItemTagBinding
).
Hope it helps someone:)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.