-2

I have this Fragments but i cant findviewbyid. i already look around website but get nothing. thankss

public class Course extends Fragment implements OnClickListener

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

    View view = inflater.inflate(R.layout.course, container, false);
    View button1Button = findViewById(R.id.button_button1);
    button1Button.setOnClickListener(this);
    View button2Button = findViewById(R.id.button_button2);
    button2Button.setOnClickListener(this);
    View button3Button = findViewById(R.id.button_button3);
    button3Button.setOnClickListener(this);
    View button4Button = findViewById(R.id.button_button4);
    button4Button.setOnClickListener(this);
    View button5Button = findViewById(R.id.button_button5);
    button5Button.setOnClickListener(this);
    View button6Button = findViewById(R.id.button_button6);
    button6Button.setOnClickListener(this);
    View button7Button = findViewById(R.id.button_button7);
    button7Button.setOnClickListener(this);
    View button8Button = findViewById(R.id.button_button8);
    button8Button.setOnClickListener(this);
}

thankss

6 Answers6

0

Fragment does not have method findViewById(), instead use the layout you are inflating.

change

View button1Button = findViewById(R.id.button_button1);

to

View button1Button = view.findViewById(R.id.button_button1);

and all other assignments.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
0

Just do following change. It should work after that.

Button button8Button = view.findViewById(R.id.button_button8);
Mayank Pandya
  • 265
  • 3
  • 14
0

Simple, you need to call findViewById on the inflated view:

View button1Button = view.findViewById(R.id.button_button1);

Don't forget to return your view variable at the end of the method.

npace
  • 4,218
  • 1
  • 25
  • 35
0

It should be like this.

public class Course extends Fragment implements OnClickListener

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

        View view = inflater.inflate(R.layout.course, container, false);
        View button1Button = view.findViewById(R.id.button_button1);
        button1Button.setOnClickListener(this);
        //Other buttons go here

        return view;
    }
}
vidulaJ
  • 1,232
  • 1
  • 16
  • 31
0

try to this hope this can help you..

 public class Course extends Fragment implements OnClickListener

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

    View view = inflater.inflate(R.layout.course, container, false);
    Button button1Button = (Button) view.findViewById(R.id.button_button1);
    button1Button.setOnClickListener(this);
    Button button2Button = (Button) view.findViewById(R.id.button_button2);
    button2Button.setOnClickListener(this);
    Button button3Button = (Button) view.findViewById(R.id.button_button3);
    button3Button.setOnClickListener(this);
    Button button4Button = (Button) view.findViewById(R.id.button_button4);
    button4Button.setOnClickListener(this);
    Button button5Button = (Button) view.findViewById(R.id.button_button5);
    button5Button.setOnClickListener(this);
    Button button6Button = (Button) view.findViewById(R.id.button_button6);
    button6Button.setOnClickListener(this);
    Button button7Button = (Button) view.findViewById(R.id.button_button7);
    button7Button.setOnClickListener(this);
    Button button8Button = (Button) view.findViewById(R.id.button_button8);
    button8Button.setOnClickListener(this);
    return view;
}
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
0
  1. Add View in front of OnClickListener to View.OnClickListener.
  2. Add view in front of findViewById to view.findViewById.

try this:

    public class Course extends Fragment implements View.OnClickListener{

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

        View view = inflater.inflate(R.layout.course, container, false);
        View button1 = view.findViewById(R.id.button_button1);
        button1.setOnClickListener(this);
     }
      ....
}
K.Sopheak
  • 22,904
  • 4
  • 33
  • 78