Logcat Message :
java.lang.IllegalStateException: Could not find method #={() -> viewModel.onBtnClicked()(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button'
File1 : activity_main.xml
<data>
<variable
name="viewModel"
type="com.wingsquare.databindingdemo.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick = "#={() -> viewModel.onBtnClicked()"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
File 2 : MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
// val binding = ActivityMainBinding.inflate(layoutInflater)
val mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
binding.viewModel = mainViewModel
binding.lifecycleOwner = this
}
}
File 3 : MainViewModel.kt
class MainViewModel : ViewModel() {
fun onBtnClicked() {
Log.d("MainViewModel", "MainViewModel")
}
}
The Logcat Message is some how misleading.
I was facing the same error for couple of hours checking everything else I thought it be the root cause. But for this particular error, all you have to do is to keep an eye on "{ }". This is one of the drawbacks of data binding in android. lots of the times you don't get any error on compile times, And if you do! that's not really helpful !
and another thing to consider is that '=' in
android:onClick = "#={() -> viewModel.onBtnClicked()}"
is used for a two way binding.
you don't need it in this case.
you can read about it here on android official docs
android:onClick="#{() -> viewModel.onBtnClicked()}"
Add a parenthesis at the end.
also you may want to remove the equal sign after the #
Related
I am learning DataBinding in android studio. But I am facing a problem with binding a ModelView. I want to bind a function with a button on click event. I set a function in the model view. I want to update my text view with on click event of my button. But When I click the button my text is not updating. I can not understand what I have done wrong.
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="model"
type="com.example.jetpack.MainModelView" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{model.title}"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Update Text"
android:onClick="#{()-> model.update()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Main Model View:
class MainModelView : ViewModel() {
var title: String = " This is My Application"
fun update() {
title = "I am Changed"
Log.d("UPDATE", "update successfully from main model view")
}
}
Main Activity:
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
lateinit var mainModelView: MainModelView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
mainModelView = ViewModelProvider(this).get(MainModelView::class.java)
binding.model = mainModelView
}
}
My app Image:
thanks in advance for helping.
The title variable in ViewModel needs to be a ObservableField or LiveData otherwise you xml will never know when it's value got updated –
class MainViewModel : ViewModel() {
var text = MutableLiveData(" Welcome to my application ")
fun updateText() {
text.value = " Text is updated successfully "
}
}
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
// Creating MainViewModel object
mainViewModel = MainViewModel()
// Binding mainViewModel variable
// with MainViewModel object
binding.mainViewModel = mainViewModel
// passing LifeCycleOwner to binding object
binding.lifecycleOwner = this
}
}
I'm trying to get viewBinding to work.
This is the code:
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view: ConstraintLayout = binding.root
setContentView(view)
}
I manually declared the type ConstraintLayout because it's what I'm using for that activity called AskProfileImage:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#303030"
tools:context=".AskProfileImage">
<ImageView
android:id="#+id/imageView97545"
android:layout_width="108dp"
android:layout_height="64dp"
android:layout_marginTop="8dp"
android:background="#00FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo2" />
But I'm getting the error
Type mismatch: inferred type is DrawerLayout but ConstraintLayout was expected
on
binding.root
how to fix this?
viewBinding creates a class for each activity. So in my case for this activity it's:
ActivityAskProfileImageBinding
So the correct code is:
private lateinit var binding: ActivityAskProfileImageBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAskProfileImageBinding.inflate(layoutInflater)
setContentView(binding.root)
}
Since viewBinding is probably broadly used now, I don't understand why the hell nobody could answer that and why google docs are as always useless
I've tried multiple solutions and read some other answers talking about the visibility of the class, but I've no added any private or protected modifier.
The point is, that I'm just have a very simple ViewBinding configuration, but it's showing the following message when I try to run it.
e: /app/src/main/java/com/adalpari/example/view/MainActivity.kt: (16,40): Cannot access '': it is private in 'ActivityMainBinding'
e: /app/src/main/java/com/adalpari/example/view/MainActivity.kt: (16,60): No value passed for parameter 'rootView'
e: /app/src/main/java/com/adalpari/example/view/MainActivity.kt: (16,60): No value passed for parameter 'progressBar'
e: /app/src/main/java/com/adalpari/example/view/MainActivity.kt: (16,60): No value passed for parameter 'storiesView'
The code, as you can see is pretty simple..
MainActivity:
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
val binding: ActivityMainBinding = ActivityMainBinding()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
onObserve()
}
...
}
xml file:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity"
>
<com.adalpari.storiesview.view.StoriesView
android:id="#+id/stories_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="#+id/stories_view"
app:layout_constraintEnd_toEndOf="#+id/stories_view"
app:layout_constraintStart_toStartOf="#+id/stories_view"
app:layout_constraintTop_toTopOf="#+id/stories_view"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
And finally the enabling of ViewBinding in app's gradle:
apply plugin: 'kotlin-kapt'
android {
...
buildFeatures {
viewBinding true
}
...
}
Any help is very appreciated, thanks in advance!
You're not supposed to use the constructors of data binding classes (they're private, as your error message says). Use the method ActivityMainBinding.inflate() to create your binding object
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
This is shown in the official docs for ViewBinding
I would recommend using dataBinding which includs viewBinding and other powerful features you may need in the future.
1. Enable dataBinding in Build Gradle File:
apply plugin: 'kotlin-kapt'
android {
...
buildFeatures {
dataBinding true
}
...
}
2. Wrap the Whole Layout inside <layout> Tag:
<layout //key point
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
...
</LinearLayout>
</layout>
3. Setup Activity/Fragment:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding //may need rebuild project
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//binding.xxx.xxx
}
}
For Fragment:
binding: FragmentFirstBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false)
I am relatively new to kotlin and trying to build a project with data binding in some fragments.I have a fragment named UserFragment with a Recyclerview in it like this:
class UserFragment : Fragment() {
private lateinit var binding: FragmentUserBinding
private lateinit var viewModel: UserListViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding=DataBindingUtil.inflate(inflater,R.layout.fragment_user, container, false)
binding.userRecycler.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
viewModel = ViewModelProviders.of(this).get(UserListViewModel::class.java)
viewModel.errorMessage.observe(this, Observer {
errorMessage -> if(errorMessage != null) showError(errorMessage) else hideError()
})
binding.mViewModel=viewModel
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//start add activity
val i= Intent(activity,AddUserActivity::class.java)
userFab.setOnClickListener(View.OnClickListener {
startActivity(i)
})
}
private fun showError(#StringRes errorMessage:Int){
errorSnackbar = Snackbar.make(binding.root, errorMessage, Snackbar.LENGTH_INDEFINITE)
errorSnackbar?.setAction(R.string.retry, viewModel.errorClickListener)
errorSnackbar?.show()
}
private fun hideError(){
errorSnackbar?.dismiss()
}
}
and the xml layout file fragment_user.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layout
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">
<data>
<variable
name="mViewModel"
type="com.example.***.ui.User.UserListViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/userDateEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/pick_date"
android:background="#drawable/roundededittext"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userRecycler"
android:layout_width="293dp"
android:layout_height="475dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/userDateEditText"
app:adapter="#{viewModel.getUserListAdapter()}"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/userFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginBottom="48dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:mutableVisibility="#{viewModel.getLoadingVisibility()}"
android:id="#+id/userProgressBar" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="140dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintVertical_bias="0.499"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
there is also a similar adapter class and item_user.xml:
class UserListAdapter : RecyclerView.Adapter<UserListAdapter.ViewHolder>() {
private lateinit var userModelList:List<UserModel>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserListAdapter.ViewHolder {
val binding: ItemUserBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_user, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: UserListAdapter.ViewHolder, position: Int) {
holder.bind(userModelList[position])
}
override fun getItemCount(): Int {
return if(::userModelList.isInitialized) userModelList.size else 0
}
fun updateUserList(userModelList:List<UserModel>){
this.userModelList = userModelList
notifyDataSetChanged()
}
class ViewHolder(private val binding: ItemUserBinding):RecyclerView.ViewHolder(binding.root){
private val viewModel = UserViewModel()
fun bind(userModel: UserModel){
viewModel.bind(userModel)
binding.viewModel =viewModel
}
}
}
the item-user.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.***.ui.MyUser.UserViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/user_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
app:mutableText="#{viewModel.getUserTitle()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/user_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:mutableText="#{viewModel.getUserDesc()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_title" />
</android.support.constraint.ConstraintLayout>
</layout>
note that databinding has been enabled in the gradle
and the very important issue here is that in both my fragment and the adapter
this line:
binding.viewModel =viewModel
reports a type mismatch like this:
Type mismatch.
Required:MyUser.UserListViewModel?
Found:com.example.***.ui.MyUser.UserListViewModel
and when i build my project the error is as follows:
error: cannot find symbol
import com.example.***.databinding.FragmentUserBindingImpl;
The first error is pretty explicit: binding.viewModel expects a nullable UserListViewModel? and get a non-nullable UserListViewModel(see kotlin null safety doc).
You can try something like this to get rid of it:
Declare your viewModel as
private var viewModel: UserListViewModel? = null
and set your binding this way:
viewModel?.let{binding.viewModel = it}
Concerning the second error, your declarations seems fine, but sometimes the Android Studio's cache get corrupted, try Invalidate Caches/Restart, it may help.
Type mismatch.
Required:MyUser.UserListViewModel?
Found:com.example.***.ui.MyUser.UserListViewModel
Basically the error is telling that your
binding.viewModel //is a nullable type and there for it expects a nullable
//type to be assigned as well
So just turn your view model into a nullable type by addind the ? simbol after its delacarion (note late init types do not allow nullable types). try it like this
private var viewModel: UserListViewModel? = null
About the second error data binding library need to compile in order to autogenerate the binding class, just rebuild the project and this error will be gone.
Please feel free to use this template as a base to avoid all that boilerplate code
#Retention(AnnotationRetention.RUNTIME)
#Target(AnnotationTarget.CLASS)
annotation class ContentView(#LayoutRes val id: Int)
fun ViewGroup.inflate(#LayoutRes layoutId: Int,
addContainer: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutId,this,addContainer)
}
#Suppress("UNCHECKED_CAST")
abstract class BaseFragment<Model : ViewModel, Binding : ViewDataBinding> : Fragment() {
/**
* Data binding class variable all view elements declared on the
* Xml file will be available within this instance if a view model
* Is required for the xml to work we will need to bind it on #onBindViewModel
*/
protected lateinit var binding: WeakReference<Binding?>
/**
* Fragments view model according to MVVM android architecture
* Each fragment class should have one , in order to facilitate
* Live Data and Binding library features, tho we can skip it
*/
protected lateinit var viewModel: WeakReference<Model?>
/**
* Here is where most likely you will get not null data , both binding and
* view model references can be destroyed by garbage collector
* If this application reaches low memory levels
*
* This optional method is used to bind the required view model inside the
* Xml file, this is optional to use tho is recommended
* Bind them by calling the view model binding.customViewModel = viewModel
*/
protected open fun onBindViewModel(viewModel: Model?, binding: Binding?) {}
/**
* There will be the occasion where custom params will be needed on view model's
* Constructor in this case will want to override the default creation #see ViewModelFactory
*/
protected open fun onCreateViewModel(modelType: Class<Model>): Model? = if (modelType != ViewModel::class.java)
ViewModelProviders.of(requireActivity()).get(modelType) else null
/**
* Here we will inherit view model and binding values based on the class
* Parameters and store them in global variables so any class extending
* From Base activity has access to binding and view model instances by default
*/
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val layout = this.javaClass.annotations.find { it.annotationClass == ContentView::class } as ContentView?
?: throw RuntimeException("You annotate this class with #ContentView and provide a layout resource")
container?.let { binding = WeakReference(DataBindingUtil.bind(it.inflate(layout.id))!!) }
?: run { binding = WeakReference(DataBindingUtil.bind(inflater.inflate(layout.id, null))) }
viewModel = WeakReference(
onCreateViewModel(
(this.javaClass.genericSuperclass
as ParameterizedType).actualTypeArguments[0] as Class<Model>
)
)
setHasOptionsMenu(true)
onBindViewModel(viewModel.get(), binding.get())
return binding.get()?.root
}
}
And just use it like this (see how much boiler plate code is gone)
#ContentView(R.layout.fragment_user)
class UserFragment: BaseFragment<UserListViewModel, FragmentUserBinding> {
override fun onBindViewModel(viewModel: UserListViewModel?, binding: FragmentUserBinding?) {
binding.viewModel = viewModel
}
}
I have created sample app to demo my question:
A TextView and a Button, text view visibility is bound to viewModel.bar
I want the button to switch the value of viewModel.bar when clicked and the UI to get updated as well.
However, this is not happening. The value is changed but the UI is not updated.
Layout File:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable name="viewModel"
type="com.example.bindingone.MainViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:visibility="#{viewModel.bar ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:text="Update UI"
android:onClick="clicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
</layout>
MainActivity File:
class MainActivity : AppCompatActivity() {
private val viewModel = MainViewModel()
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.viewModel = viewModel
}
fun clicked(v: View) {
viewModel.bar.value = viewModel.bar.value?.not() ?: false
}
}
MainViewModel file:
class MainViewModel : ViewModel() {
var bar = MutableLiveData<Boolean>()
}
After creating binding, add this line binding.setLifecycleOwner(this).
/**
* Sets the {#link LifecycleOwner} that should be used for observing changes of
* LiveData in this binding. If a {#link LiveData} is in one of the binding expressions
* and no LifecycleOwner is set, the LiveData will not be observed and updates to it
* will not be propagated to the UI.
*
* #param lifecycleOwner The LifecycleOwner that should be used for observing changes of
* LiveData in this binding.
*/
#MainThread
public void setLifecycleOwner(#Nullable LifecycleOwner lifecycleOwner)