[英]Floating Action Button null when inside a coordinatorlayout
我在協調器布局中有一個 FAB。 目標是將 FAB 錨點放置到具有底部導航視圖作為子項的應用欄上,但 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 的情況下工作得很好,並閱讀了 developer.android 上關於 coordinatorlayout 的文檔我對我在這里的錯誤一無所知。 任何幫助,將不勝感激。
不知什么原因,問題出在用class創建的xml文件中,經過多次測試,創建一個新的activity,復制上一個activity的代碼,問題解決了,所以我創建了一個單獨的 XML 文件。 以前不能使用舊界面的類的上下文,所以一切都完美無缺,但是我仍然對這個問題很感興趣,因為我將另一個文件中的 XML 代碼復制並粘貼到新文件中,它的工作原理如下這應該。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.