T
T
TweeM4N2021-04-21 23:46:55
Java
TweeM4N, 2021-04-21 23:46:55

How to call a private method in Fragmente Android Studio?

It is not possible to call private void userLogin() in case 2. The application closes when the Login button is pressed.

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register:
                Intent reg = new Intent(getActivity(), Register.class);
                startActivity(reg);
                break;

            case R.id.signIn:
                userLogin();
                break;

            case R.id.forgotPassword:
                Intent fP = new Intent(getActivity(), ForgotPassword.class);
                startActivity(fP);
                break;
        }
    }


Whole code:
public class ProfileFragment extends Fragment implements View.OnClickListener {

    private TextView register, forgotPassword;
    private EditText editTextEmail, editTextPassword;
    private Button signIn;

    private FirebaseAuth mAuth;
    private ProgressBar progressBar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mAuth = FirebaseAuth.getInstance();

        View v = inflater.inflate(R.layout.fragment_profile, container, false);

        TextView register = (TextView) v.findViewById(R.id.register);
        register.setOnClickListener(this);

        Button signIn = (Button) v.findViewById(R.id.signIn);
        signIn.setOnClickListener(this);

        EditText editTextEmail = (EditText) v.findViewById(R.id.email);
        EditText editTextPassword = (EditText) v.findViewById(R.id.password);

        ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressBar);

        TextView forgotPassword = (TextView) v.findViewById(R.id.forgotPassword);
        forgotPassword.setOnClickListener(this);
        return v;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register:
                Intent reg = new Intent(getActivity(), Register.class);
                startActivity(reg);
                break;

            case R.id.signIn:
                userLogin();
                break;

            case R.id.forgotPassword:
                Intent fP = new Intent(getActivity(), ForgotPassword.class);
                startActivity(fP);
                break;
        }
    }

    private void userLogin() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if(email.isEmpty()){
            editTextEmail.setError("Email обязателен!");
            editTextEmail.requestFocus();
            return;
        }

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            editTextEmail.setError("Пожалуйста, введите корректный email!");
            editTextEmail.requestFocus();
            return;
        }

        if(password.isEmpty()){
            editTextPassword.setError("Пароль обязателен!");
            editTextPassword.requestFocus();
            return;
        }

        if(password.length() < 6){
            editTextPassword.setError("Пароль должен состоять минимум из 6 символов!");
            editTextPassword.requestFocus();
            return;
        }

        progressBar.setVisibility(View.VISIBLE);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Vodakov, 2021-04-22
@TweeM4N

In the onCreateView method, instead of

EditText editTextEmail = (EditText) v.findViewById(R.id.email);
        EditText editTextPassword = (EditText) v.findViewById(R.id.password);

Need
editTextEmail = (EditText) v.findViewById(R.id.email);
        editTextPassword = (EditText) v.findViewById(R.id.password);

Let me explain:
You have declared variables within the class private EditText editTextEmail, editTextPassword;
Then, in the onCreateView method, when assigning values ​​to them, write EditText before the variable name. Thus, you declare new variables within the scope of the method, and the class variables remain uninitialized, i.e. null.
Then, after the call, in the userLogin method , you access the editTextEmail.getText() variable , but editTextEmail contains null and such a call causes a Null Point Exception, which looks like just closing the application to the user.
And not the fact that this is the only mistake.

J
Jacen11, 2021-04-22
@Jacen11

Can't call private void userLogin() in case 2.

And what does it mean? here you are called, quite to yourself
case R.id.signIn:
                userLogin();
                break;

in terms of language everything is ok. Well, you need to debug and read the logs

O
Orkhan, 2021-04-22
Hasanly @azerphoenix

The problem is not that you are not calling the method, since it is explicitly called here:

case R.id.signIn:
                userLogin();
                break;

The problem is that after calling the method, your application crashes.
The application closes when the Login button is pressed.

Try to debug the application and look at the logs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question