Issue
So, findViewbyId does not work in this case, binding does not seem to work as well, at least I haven't found a working solution to it, and I'm stuck. I've read countless answers here but none of them seem to do the trick since it looks like it's different for fragments.
Please do not answer with kotlin-android-extension, that is deprecated and I'd like to not use it since most of my code is already under binding.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
frameLayout = inflater.inflate(R.layout.fragment_augmented_face, container, false) as FrameLayout
surfaceView = frameLayout?.findViewById<View>(R.id.surface_view) as GLSurfaceView
surfaceView?.let {
it.preserveEGLContextOnPause = true
it.setEGLContextClientVersion(2)
it.setEGLConfigChooser(8, 8, 8, 8, 16, 0) // Alpha used for plane blending.
it.setRenderer(this)
it.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
it.setWillNotDraw(false)
}
return frameLayout
}
These two lines here, they are unresolved references:
frameLayout = inflater.inflate(R.layout.fragment_augmented_face, container, false) as FrameLayout
surfaceView = frameLayout?.findViewById<View>(R.id.surface_view) as GLSurfaceView
I'm not sure if it is because it's a Fragment and not AppCompatActivity that I can't make it work? What are my options here?
Edit here to clarify what I've tried:
I use instead id 'kotlin-parcelize', and viewBinding true
Also tried the Fragment binding instructions on the documentation, so it would look like this.:
lateinit var _binding: AugmentedFaceFragment
private val binding get() = _binding!!
...
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
_binding = AugmentedFaceListener.inflate(inflater, container, false)
And now I get unresolved reference on inflate. (Even before adding the rest of the code).
Seems apart from writing the right way here, I was also setting the wrong name of the binding, instead of the xml file I was writing the class name. Here is the working code:
private var _binding: FragmentAugmentedFaceBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentAugmentedFaceBinding.inflate(inflater, container, false)
frameLayout = binding.root
surfaceView = binding.surfaceView as GLSurfaceView
surfaceView?.let {
it.preserveEGLContextOnPause = true
it.setEGLContextClientVersion(2)
it.setEGLConfigChooser(8, 8, 8, 8, 16, 0) // Alpha used for plane blending.
it.setRenderer(this)
it.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
it.setWillNotDraw(false)
}
return frameLayout
}
//Also need to remember to nullify the binding onDestroy:
override fun onDestroy() {
_binding = null
super.onDestroy()
}
Solution
Did you get rid of the kotlin-android-extension
declaration? I think that would be the problem on why it cannot find these method declration.
Either way, you should really use viewBinding/dataBinding instead of this old-school way of grabbing onto your UI components. Is a lot easier to use and much more convenient.
Here are some links
View binding: https://developer.android.com/topic/libraries/view-binding
Data binding: https://developer.android.com/topic/libraries/data-binding
Choose your weapons wisely!
UPDATE:
While using viewBinding
, all of your XML classes should be automatically generating a `binding class that you can then use to access your UI components.
For the example above with a fragment called AugmentedFaceFragment
, you should be able to access your binding class and inflate it by calling:
_binding = AugmentedFaceFragmentBinding.inflate(inflater, container, false)
Answered By - Ivan Garza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.