无法解析片段中的 findViewById

Cannot resolve findViewById in fragment

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

有什么我可以在 Fragment 中使用 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);
}

在查找之前添加 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; 在里面。也需要把它移到底部。

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;

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

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 是函数的最后一条语句。

无法访问 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;

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

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

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

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

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