【问题标题】:NullPointerException in Floating Action Button in Fragment [duplicate]片段中浮动操作按钮中的 NullPointerException [重复]
【发布时间】:2017-10-21 05:28:03
【问题描述】:

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

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>

我在 stackoverflow 上读到,我使用了 view.findById,但它不起作用,我不明白有什么问题?

【问题讨论】:

  • 您遇到空指针异常,意味着您的 fab id 不正确或者它只是在另一个布局中定义。
  • 是的。 findViewById 方法返回 null,因为找不到元素
  • 你可能会使用即时运行吗?

标签: android android-fragments nullpointerexception floating-action-button android-handler


【解决方案1】:

看来您的代码是正确的。我发现FloatingActionButton 说: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。

【讨论】:

    【解决方案2】:

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

    android:onClick:"nameOfMethod"
    

    在你的片段中:

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

    【讨论】:

    • 永远不要在 XML 中添加监听器。你会为此在地狱中燃烧。
    • 为什么会这样?
    • 因为在您更改与它们相关的至少一小部分代码后,它们会立即被开发人员丢失。 stackoverflow.com/a/18267474/1568530google.com.ua/…
    • 我一直在使用它们,从来没有遇到过问题。谁知道为什么...
    • 我明白了你的意思,但他们首先做到了!
    猜你喜欢
    • 2019-09-15
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2016-08-26
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多