【问题标题】:Cannot resolve findViewById in fragment [duplicate]无法解析片段中的 findViewById [重复]
【发布时间】:2019-05-03 21:15:34
【问题描述】:

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

【问题讨论】:

    标签: android android-studio fragment findviewbyid


    【解决方案1】:

    首先,您在 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 是函数的最后一条语句。

    【讨论】:

      【解决方案2】:

      你可以使用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);
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • @m-andre-juliansyah 在你的 onCreateView() 中这样做
        【解决方案4】:

        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;
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-11-25
            • 1970-01-01
            • 1970-01-01
            • 2021-10-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-22
            相关资源
            最近更新 更多