尝试为FAB使用侦听器时出现NullPointerException

时间:2015-09-18 02:04:58

标签: android nullpointerexception android-support-library floating-action-button

我正在尝试将FAB放入ViewPager选项卡中,但是我收到此错误(仅限于此):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

当我在activity_main.xml

中使用FAB时,它很好

MainActiviy:

private FloatingActionButton fabButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   ...
// Initializing Floating Action Button
    fabButton = (FloatingActionButton) findViewById(R.id.fabButton);
    fabButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "FAB was pressed!", Snackbar.LENGTH_SHORT)
                    .setAction("Undo", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    })
                    .show();
        }
    });
...
}

PageFragment用于对不同的标签进行排序:

public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

public static PageFragment newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    PageFragment fragment = new PageFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage = getArguments().getInt(ARG_PAGE);
    System.out.println(">> mPage = " + mPage); //Testing
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    SampleFragmentPagerAdapter sampleFragmentPagerAdapter = new SampleFragmentPagerAdapter(getFragmentManager(), getContext());
    View view;
    switch (mPage) {
        case 1:
            view = inflater.inflate(R.layout.tab1_frag, container, false);
            return view;
        case 2:
            view = inflater.inflate(R.layout.tab2_frag, container, false);
            return view;
        case 3:
            view = inflater.inflate(R.layout.tab3_frag, container, false);
            return view;
        default:
            view = inflater.inflate(R.layout.fragment_page, container, false);
            return view;
    }
}

}

tab_2frag特定标签的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Tab 2"
        android:textSize="40sp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginEnd="@dimen/fab_margin_end"
        android:src="@mipmap/ic_add_white_24dp"
        app:fabSize="normal" />
</FrameLayout>

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:4)

不要使用听众。 在你的xml中:

android:onClick:"nameOfMethod"

在您的活动中:

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

答案 1 :(得分:1)

您的FloatingActionButton采用片段布局,但您正试图在MainActivity onCreate()中找到它。 findViewById(R.id.fabButton)返回null,因此在MainActivity中为其设置了点击侦听器时会导致错误。

如果您想以编程方式执行此操作,可以在片段的onCreateView()中调用它

view = inflater.inflate(R.layout.tab2_frag, container, false);
fabButton = (FloatingActionButton) view.findViewById(R.id.fabButton);
fabButton.setOnClickListener(new View.OnClickListener() {...} );
return view;