question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IllegalStateException when trying to use view with createMethod = INFLATE

See original GitHub issue

In 1.4.6, code below that uses view binding delegate with CreateMethod.INFLATE works as expected:

private val mainBinding: ActivityMainBinding by viewBinding(ActivityMainBinding::bind)

// Issue is in this binding
private val innerBinding: ItemToAddBinding by viewBinding(createMethod = CreateMethod.INFLATE)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mainBinding.rootLayout.setOnClickListener {
        // Works fine in 1.4.6, but breaks in 1.5.3. Crashes on `innerBinding.itemButton`
        mainBinding.rootLayout.addView(innerBinding.itemButton)
    }
}

In 1.5.3 this code crashes with the following exception:

java.lang.IllegalStateException: Host view isn't ready to create a ViewBinding instance
        at by.kirich1409.viewbindingdelegate.LifecycleViewBindingProperty.getValue(ViewBindingProperty.kt:87)
        at by.kirich1409.viewbindingdelegate.LifecycleViewBindingProperty.getValue(ViewBindingProperty.kt:72)
        at com.arsvechkarev.vbpdtest.MainActivity.getInnerBinding(MainActivity.kt:14)
        at com.arsvechkarev.vbpdtest.MainActivity.onCreate$lambda-0(MainActivity.kt:20)
        at com.arsvechkarev.vbpdtest.MainActivity.$r8$lambda$_81mDixuFKw3tx-DyH6RhN9DAAU(Unknown Source:0)
        at com.arsvechkarev.vbpdtest.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I suspect that the problem is either in isViewInitialized method in ActivityViewBindingProperty, or in my code above. Any idea on how to fix this?

P.S I created a sample project where you can successfully reproduce this issue

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
kirich1409commented, Jan 6, 2022

In 1.5.6 I added functions that isn’t connected to Lifecycle of Activity. You can use them viewBindingLazy() or viewBindingWithLifecycle()

0reactions
kirich1409commented, Jan 27, 2022

In your case viewBinding delegate by default connected with lifecycle of Activity. If it normal for you, continue to use it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Java illegal state Exception when trying to add View ...
1 Answer 1 ... @Badrinath That is because you use a RelativeLayout in the main.xml file instead of a LinearLayout with orientation set...
Read more >
[Bug] CollectionView throws java.lang.IllegalStateException ...
Description I'm trying to use RemainingItemsThreshold and RemainingItemsThresholdReachedCommand to load more data on demand but I'm seeing ...
Read more >
News — JRuby.org
Initial support for Rails 7. SQLite and MySQL are largely functional, but work remains to update them and PostgreSQL. JRuby 9.3.9.0 Released. Monday,...
Read more >
'Check for updates now' throws 'Connection failed' error ...
It comes back with the error 'Connection failed. Please check your network connection and try again.' I can confirm that there is a...
Read more >
android.view.InflateException: Binary XML file line #17 Code ...
InflateException : Binary XML file line when use ?attr/ ... IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found