繁体   English   中英

Android浮动操作按钮null

[英]Android Floating Action Button null

在我的布局fragment_do_stuff.xml中,我像这样添加了FloatingActionButton

<com.melnykov.fab.FloatingActionButton
    android:id="@+id/fabDoStuff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="12dp"
    android:layout_marginBottom="12dp"
    android:src="@drawable/my_fab"
    android:scaleType="centerInside"
    fab:fab_colorNormal="@color/fab_color"
    fab:fab_colorPressed="@color/fab_color"
    fab:fab_colorRipple="@color/my_accent"/>

在我的片段中,我在onCreateView()中将此视图膨胀为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_do_stuff, container, false);

    mDoStuffFab = (FloatingActionButton)view.findViewById(R.id.fabDoStuff);

    return view;
}

在我的onActivityCreated()中,我在此FAB上设置侦听器,例如:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ...

    mDoStuffFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doStuff();
        }
    });

    ...
}

但是,在fab mDoStuffFab上咆哮表明,在onCreateView和onActivityCreated中,它均为null。 结果,我的应用因NullPointerException而崩溃

我缺少什么?

谢谢,

这是我的错误,findViewById正在寻找错误的晶圆厂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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