在coordinatorlayout中浮动动作按钮为空



我在coordinatorlayout中有一个FAB。目标是把一个FAB锚放到一个appbar上,它有一个底部导航视图作为子视图,但是FAB只是不出现在屏幕上,并在类上返回一个空对象。coordinatorlayout在FragmentContainerView的相同范围内,我猜视图与coordinatorlayout冲突;

下面是我的XML代码:

<RelativeLayout 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"
android:background="@color/white"
tools:context="Main.Main_Screen"
>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_main"
/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/main_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="@color/Laranja">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="@color/Transparente"
android:outlineSpotShadowColor="@color/Transparente"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/main_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_fast_sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:src="@drawable/icones"
app:backgroundTint="@color/Azul"
app:borderWidth="0.7dp"
app:layout_anchor="@id/main_bottom_bar"
app:maxImageSize="38dp"
app:tint="@null"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>

下面是我试图实例化FAB变量的代码片段:

fab_fast_sync = findViewById(R.id.fab_fast_sync);
fab_fast_sync.setOnClickListener(this); //returns a error by null object reference

我在其他项目上做了一个测试,没有FragmentContainerView,它的工作非常好,并阅读了开发人员关于coordinatorlayout的文档。我对我的错误毫无头绪。如有任何帮助,不胜感激。

由于某种原因,我仍然不知道,问题是在用类创建的XML文件中,经过几次测试,创建了一个新活动并复制了前一个活动的代码,问题得到了解决,所以我创建了一个单独的XML文件。类的上下文以前不能与旧的接口一起工作,所以一切都工作得很好,但是我仍然对这个问题很感兴趣,因为我从另一个文件复制并粘贴了XML代码到新文件中,它正常工作。

最新更新