0

I've created a viewpager with 3 pages. Currently one of them has a button, but when I try to add a button listener to the button the app crashes but debug doesn't report any errors. Other than disconnecting to the virtual machine. Originally the view was inflated and the listener added inside the case statement itself but that didn't work either.

If the Android has a problem shouldn't the console say something?

The fragment and its behaviour are below the app crashes on setOnClickListener. any ideas? Thank you

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/app_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />

    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintVertical_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

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

            View rootView;
            View mainView = inflater.inflate(R.layout.fragment_main, container, false);
            button= (Button) getActivity().findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Code here executes on main thread after user presses button
                    TextView textView = (TextView)getActivity().findViewById(R.id.section_label);
                    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                }
            });

            int page=getArguments().getInt(ARG_SECTION_NUMBER);
            switch (page)
            {
                case 1:
                    rootView=mainView;

                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.activity_options, container, false);
                    break;
                case 3:
                    rootView = inflater.inflate(R.layout.activity_request, container, false);
                    break;
                    default:
                        rootView = inflater.inflate(R.layout.fragment_main, container, false);
                        break;

            }


            return rootView;
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
fjb59
  • 19
  • 2
  • *`adding button listener to viewpager crashes app`* where is your **`crash-log`** you need to share **`crash-log`** with question – AskNilesh May 03 '18 at 08:07
  • button= (Button) mainView.findViewById(R.id.imageButton2); – will May 03 '18 at 08:11

4 Answers4

1
  1. wrong findViewById

Try this

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

            View rootView;
            View mainView= inflater.inflate(R.layout.fragment_main, container, false);
          ImageButton  button= (ImageButton) mainView.findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Code here executes on main thread after user presses button
                    TextView textView = (TextView)mainView.findViewById(R.id.section_label);
                    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                }
            });

            int page=getArguments().getInt(ARG_SECTION_NUMBER);
            switch (page)
            {
                case 1:
                    rootView=mainView;

                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.activity_options, container, false);
                    break;
                case 3:
                    rootView = inflater.inflate(R.layout.activity_request, container, false);
                    break;
                    default:
                        rootView = inflater.inflate(R.layout.fragment_main, container, false);
                        break;

            }


            return rootView;
        }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

There can be more than one case where that would go wrong, one of them i think it is that ImageButton cannot be cast to Button. If that is not the case try with the debugger to see what comes null(most probably something is null)

Alexandru Sandu
  • 314
  • 2
  • 13
1

Change the following

button= (Button) getActivity().findViewById(R.id.imageButton2); 

to

button= (Button) mainView.findViewById(R.id.imageButton2);

And also initialize your button after inflating the view. Because it might not create problem in initializing but will be crash if your view changes where your actual button was.

Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
0

thanks to the answers above the solution was to change one line from button= (Button) getActivity().findViewById(R.id.imageButton2); to

ImageButton button= mainView.findViewById(R.id.imageButton2);

fjb59
  • 19
  • 2