gpt4 book ai didi

android - 将FAB链接到FAB行为类

转载 作者:行者123 更新时间:2023-12-02 12:55:13 25 4
gpt4 key购买 nike

我正在尝试将我的FAB链接到FAB Behavior类,以便轻扫 Action 隐藏FAB。应用程序将在启动时崩溃,我认为问题出在FAB的layout_behavior元素上,当我从XML中删除该行时应用程序罚款。日志在下面列出。
在这里,我尝试了一些教程。
https://guides.codepath.com/android/floating-action-buttons
https://code.luasoftware.com/tutorials/android/android-hide-floatingactionbutton-on-scroll-down/
https://www.dev2qa.com/android-hide-floating-action-button-while-recyclerview-scroll-example/

dependencies {
...
implementation 'com.google.android.material:material:1.2.1'

}
当前在Mainactivty中没有任何内容。
activity_main.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
tools:context=".MainActivity">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"

// I believe the probelm is here
app:layout_behavior="com.example.deletefab2.MyFab"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
FAB行为类(class)
package com.example.deletefab2

class MyFab : FloatingActionButton.Behavior() {
// this secion i
override fun nestedScroll( ...) {
Log.i("FAB behavior", "its working")
}

}

我的理解是FAB应该通过layout_behavior链接到Behavior类,对于我来说应该从Activity调用它似乎有点奇怪。
日志记录
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.deletefab2, PID: 18065
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deletefab2/com.example.deletefab2.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.example.deletefab2:layout/activity_main: Could not inflate Behavior subclass com.example.deletefab2.MyFab
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
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)
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.deletefab2:layout/activity_main: Could not inflate Behavior subclass com.example.deletefab2.MyFab
Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.example.deletefab2.MyFab
at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:649)
at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2896)
at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1740)
at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.deletefab2.MainActivity.onCreate(MainActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
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)
Caused by: java.lang.NoSuchMethodException: com.example.deletefab2.MyFab.<init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:643)
at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2896) 
at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1740) 
at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:682) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:534) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:481) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) 
at com.example.deletefab2.MainActivity.onCreate(MainActivity.kt:9) 
at android.app.Activity.performCreate(Activity.java:7802) 
at android.app.Activity.performCreate(Activity.java:7791) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
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) 


最佳答案

因为行为类是在XMXL中定义的,所以您必须使用上下文和AttributeSet实现构造函数,以调用与layout_behavior链接的类。

class MyFABBehaviorclass (context: Context, attrs: AttributeSet): FloatingActionButton.Behavior() {
...
}

关于android - 将FAB链接到FAB行为类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64288751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com