片段中浮动操作按钮中的 NullPointerException

NullPointerException in Floating Action Button in Fragment

你能帮我解决这个错误吗:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                    at com.sadface.tutorapp.FirstFragment.onCreateView(FirstFragment.java:23)

我想在片段中处理 FAB,所以这里是代码:

FirstFragment.class

public class FirstFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_first,
            container, false);
    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    return view;
}

}

和fragment_first.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark">

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="First Fragment"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_input_add" /></RelativeLayout>

我在 Whosebug 上看到,我使用过 view.findById,但它不起作用,我不明白有什么问题?

将此添加到您在 xml 中的 FAB:

android:onClick:"nameOfMethod"

在你的片段中:

    public void nameofMethod(View v){
     //button Clicked
}

看来你的代码是对的。我发现 FloatingActionButton says:Android 浮动操作按钮对滚动事件有反应。附加目标向上滚动时可见,向下滚动时不可见。

也许你应该使用一些可以滚动的布局。这是官方的例子:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:fab="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_content_new"
        fab:fab_colorNormal="@color/primary"
        fab:fab_colorPressed="@color/primary_pressed"
        fab:fab_colorRipple="@color/ripple" />

官方例子中使用了listview