9

i am trying to get id from fragment xml so here is the code the error is " Cannot resolve the method findViewById "

Is there anyway of which I can use findViewById in Fragment ?

public class Slide_Teacher_Add extends Fragment implements View.OnClickListener {

private EditText teacher_id_number;
private EditText teacher_fullname;
private EditText teacher_lesson;
private EditText teacher_contact;

private Button btn_add_teacher;

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

    View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
    return v;

    teacher_id_number = (EditText) findViewById(R.id.teacher_id_number);
    teacher_fullname = (EditText) findViewById(R.id.teacher_fullname);
    teacher_lesson = (EditText) findViewById(R.id.teacher_lesson);
    teacher_contact = (EditText) findViewById(R.id.teacher_contact);

    btn_add_teacher = (Button) findViewById(R.id.btn_add_teacher);


    btn_add_teacher.setOnClickListener(this);
}
M Andre Juliansyah
  • 117
  • 1
  • 2
  • 14

5 Answers5

10

Firstly, you are calling findViewById() after the return statement so they wont be executed at all. Next, you need to call findViewById() on the context of a view as in view.findViewById(int); so rewrite the code as :

View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
teacher_id_number = v.findViewById(R.id.teacher_id_number); //Note this line
//other tasks you need to do
return v;

making sure the return is the last statement of the function.

Yashovardhan99
  • 887
  • 11
  • 26
4

You can use onViewCreated function without need to Inflate view. such as :

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(...);
    //....
}

Or You must call findViewById function from v object class (View). such as :

teacher_id_number = (EditText) v.findViewById(R.id.teacher_id_number);
Abbas m
  • 56
  • 3
1

Code after return can not be reached, that's first remark. Next findViewById() is not defined into Fragment you have to exctract the view from it's parent, the inflated view. So your code should be like this:

 View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);


    teacher_id_number = (EditText) v.findViewById(R.id.teacher_id_number);
    teacher_fullname = (EditText) v.findViewById(R.id.teacher_fullname);
    teacher_lesson = (EditText) v.findViewById(R.id.teacher_lesson);
    teacher_contact = (EditText) v.findViewById(R.id.teacher_contact);

    btn_add_teacher = (Button) v.findViewById(R.id.btn_add_teacher);


    btn_add_teacher.setOnClickListener(this);

   return v;
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
0

add v before find.... like this:

teacher_id_number = (EditText) v.findViewById(R.id.teacher_id_number);
teacher_fullname = (EditText) v.findViewById(R.id.teacher_fullname);
teacher_lesson = (EditText) v.findViewById(R.id.teacher_lesson);
teacher_contact = (EditText) v.findViewById(R.id.teacher_contact);

btn_add_teacher = (Button) v.findViewById(R.id.btn_add_teacher);

return v;

and you return v; inside there. need to move it to bottom as well.

Abdomns
  • 428
  • 3
  • 8
0

You need to take View's reference from the inflated layout.

    View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
    teacher_id_number = (EditText) v.findViewById(R.id.teacher_id_number);
    teacher_fullname = (EditText) v.findViewById(R.id.teacher_fullname);
    teacher_lesson = (EditText) v.findViewById(R.id.teacher_lesson);
    teacher_contact = (EditText) v.findViewById(R.id.teacher_contact);
    btn_add_teacher = (Button) v.findViewById(R.id.btn_add_teacher);
    return v;
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29