简体   繁体   中英

Setting onClicklistener() doesn't work in a fragment

Button onClick doesn't work in a fragment . What happens? The id and onClickListener function has the right syntax. I don't know what is happening.

public class LoginFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    Button btn;
    View vista = inflater.inflate(R.layout.fragment_login, container, false);
    btn = (Button)vista.findViewById(R.id.btn_login);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vista) {
            //cargarWebService();
            //name.setText("");
            //email.setText("");
            Toast.makeText(getContext(), "Se ah registrado exitosamente", Toast.LENGTH_SHORT).show();
        }
    });
    return inflater.inflate(R.layout.fragment_login, container, false);
}
}

change this

 return inflater.inflate(R.layout.fragment_login, container, false);

to

 return vista;

let me know if this work or not

You don't need to inflate the view again when returning it, simply inflate it once at the beginner of the method and then return the view at the end of the method.

View view = inflater.inflate(R.layout.fragment_login, container, false);

return view;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM