作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将我的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中没有任何内容。
<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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!