I can't change value of Views by DataBinding or by Android Extensions, but it works by 'traditional way' (findViewById). Also, it throws NPE when I try by Android Extensions' way without safe call operator.
HourlyFragment.kt
class HourlyFragment : Fragment() {
private lateinit var binding: FragmentHourlyBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var rootView = inflater.inflate(R.layout.fragment_hourly, container, false)
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_hourly, container, false
)
// doesn't work
//binding.tvHourly.text = "HOURLY!"
val tvHourly : TextView = rootView.findViewById(R.id.tv_hourly)
tvHourly.setText("HOURLY!!")
// doesn't work, without safe call operator throws NullPointerException
//tv_hourly?.text = "HOURLY!!!"
return rootView
}
}
fragment_hourly.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HourlyFragment">
<TextView
android:id="@+id/tv_hourly"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hourly fragment" />
</FrameLayout>
</layout>
question from:
https://stackoverflow.com/questions/65863480/cant-get-access-to-views-by-databinding-or-android-extensions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…