androidbroadcast / ViewBindingPropertyDelegate

Make work with Android View Binding simpler
https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719
Apache License 2.0
1.42k stars 102 forks source link

Custom view with viewBinding doesn't show in preview AS #40

Closed SergeiZavialov closed 3 years ago

SergeiZavialov commented 3 years ago

Hi. I have a trouble with my custom view where i use viewBinding delegate.

library version: implementation 'com.kirich1409.viewbindingpropertydelegate:viewbindingpropertydelegate:1.4.1'

My view code

class ItemSelectorView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attributeSet, defStyleAttr) {
    private val binding by viewBinding<ViewItemSelectorBinding>(
        createMethod = CreateMethod.INFLATE
    )
    init {
        orientation = VERTICAL
        initAttrs(attributeSet)
    }
} 

XML for custom view:

< ?xml version="1.0" encoding="utf-8"?>
< merge 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="wrap_content"
    tools:orientation="vertical"
    tools:parentTag="android.widget.LinearLayout">

<View/>

<TextView />

<TextView />

< /merge>

And when i add my view to layout - i have an "Failed to instantiate one of more classes"

java.lang.NoClassDefFoundError: Could not initialize package.presentation.custom.ItemSelectorView
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:203)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:161)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:328)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:373)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:844)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

What do i wrong?

kirich1409 commented 3 years ago

Does the custom view correctly work in runtime?

kirich1409 commented 3 years ago

What version of Android Studio did you use ?

SergeiZavialov commented 3 years ago

At runtime custom view works correctly. Trouble only with preview. I use

Android Studio 4.1.2 Build #AI-201.8743.12.41.7042882, built on December 20, 2020 Runtime version: 1.8.0_242-release-1644-b3-6915495 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.16

kirich1409 commented 3 years ago

I don't know what to do. The bug is minor. Will not be fixed. Close it for now

FirstSpectr commented 3 years ago

Have the same issue. How do you think, using delegate is necessary in custom views at all? I just do this and preview works perfectly

class MyView@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : MaterialCardView(context, attrs, defStyleAttr) {
    private val binding = MyViewBinding.inflate(LayoutInflater.from(context), this)
}
kirich1409 commented 3 years ago

Check 1.4.6. The issue must be fixed