繁体   English   中英

在协调器布局内时浮动操作按钮为空

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM