3

I want to customize a behavior based on How do I remove the bouncing effect on appbar?

But it always crashes during runtime:

2020-08-27 08:25:18.096 E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{tw.com.colatour.dev/tw.com.colatour.ui.mp.home.MpHomeActivity}: android.view.InflateException: Binary XML file line #9: Could not inflate Behavior subclass tw.com.colatour.dev.ui.app_home.ad.one.NoBounceBehavior
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6364)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
     Caused by: android.view.InflateException: Binary XML file line #9: Could not inflate Behavior subclass tw.com.colatour.dev.ui.app_home.ad.one.NoBounceBehavior
     Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass tw.com.colatour.dev.ui.app_home.ad.one.NoBounceBehavior
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:622)

    

How I use it in XML:

...
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_behavior=".ui.app_home.ad.one.NoBounceBehavior"
        android:fitsSystemWindows="true"> 
...

Here is Java code solution but I don't know how to convert it to Kotlin code.

public class NoBounceBehavior extends AppBarLayout.Behavior {
    
    public FooterBehavior() {
    }

    public FooterBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

Kotlin code:

//crash
class NoBounceBehavior(context: Context, attrs: AttributeSet) :
      AppBarLayout.Behavior(context, attrs) {

...

Another attempt which also crashes:

//crash
class NoBounceBehavior(context: Context, attrs: AttributeSet) :
      AppBarLayout.Behavior(context, attrs) {

    constructor() {}

    constructor(context: Context, attrs: AttributeSet) {}
...

GHH
  • 1,713
  • 1
  • 8
  • 21

1 Answers1

0

You have to use the fully-qualified name of your custom behavior in your XML declaration. If the name specified in XML starts with a ".", coordinator layout appends the package name at runtime. If your app appends anything to the package name (ie ".beta" or ".dev") on build, the package name won't match the package path here. Use this instead:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    app:layout_behavior="tw.com.colatour.ui.app_home.ad.one.NoBounceBehavior"
    android:fitsSystemWindows="true"> 
Egg
  • 1,986
  • 16
  • 20