无法解析片段中的findViewById

时间:2018-12-02 09:58:12

标签: android android-studio fragment findviewbyid

我正在尝试从片段xml获取ID,所以这是代码 错误是 “无法解析方法findViewById”

在片段中是否可以使用findViewById

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);
}

5 个答案:

答案 0 :(得分:0)

在找到之前添加v。...像这样:

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;

,您return v;就在里面。还需要将其移至底部。

答案 1 :(得分:0)

  

您需要从膨胀的布局中获取View的参考。

    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;

答案 2 :(得分:0)

首先,您在return语句后调用findViewById(),因此根本不会执行它们。接下来,您需要像在findViewById()中那样在视图的上下文中调用view.findViewById(int);,因此将代码重写为:

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;

确保return是函数的最后一条语句。

答案 3 :(得分:0)

无法到达return之后的代码,这是第一句话。接下来的findViewById()未定义到Fragment中,您必须从其父视图(即展开视图)中提取视图。因此,您的代码应如下所示:

 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;

答案 4 :(得分:0)

您可以使用onViewCreated功能,而无需查看Inflate。例如:

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

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

或者您必须从findViewById对象类(v)中调用View函数。例如:

teacher_id_number = (EditText) v.findViewById(R.id.teacher_id_number);