无法解析片段中的findViewById



我正试图从片段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);
}

首先,在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;

确保CCD_ 5是函数的最后一个语句。

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

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

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

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

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;

您需要从膨胀的布局中获取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;

在查找之前添加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;。也需要将其移动到底部。

最新更新