Android app crashing (fragment and xml onclick)

Activity:

If are having activity and if you define android:onClick attribute in XML then you just need to define a method with the same name in Activity.

Fragment:

But whenever you have Fragment and if you want to define click listener by just defining android:onClick attribute then you have to define a method with the same name in actual activity from where Fragment has been called.

OR you can simply implement a click listener programmatically.


You'll save yourself a lot of hassle by setting the onClickListener programmatically (instead of via XML). This should help you do what you're trying to do:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.second_fragment, container, false);
    final View button = view.findViewById(R.id.f2_button);
    button.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                /* DO SOMETHING UPON THE CLICK */
            }
        }
    );
    return view;
}