I am trying to implement On Click Listener in Fragment but it is giving me an error 'Attempt to invoke Virtual Method' I am using try/ catch to it is throwing an error in exception. Is anyone can guide me on how to implement onClick in Fragment I have different methods but all in vain. Tried onClick in XML, Tried Fragment Implement OnClickListener and btn.setOnClickListner but nor working at all.
package com.example.finalapp;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.hbb20.CountryCodePicker;
/**
* A simple {@link Fragment} subclass.
* Use the factory method to
* create an instance of this fragment.
*/
public class buyer_fragment extends Fragment {
public EditText mFirstname, mLastname, mUsername, mEmail, mPhone;
public TextInputEditText mPassword;
public CountryCodePicker ccp;
public FirebaseAuth mAuth;
public Button mSignUp;
public View view;
public buyer_fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_buyer_fragment, container, false);
initViews();
return view;
}
public void login_back(View view) {
Intent intent = new Intent(getActivity(), login.class);
startActivity(intent);
}
public void initViews() {
try {
mFirstname = view.findViewById(R.id.first_name);
mLastname = view.findViewById(R.id.last_name);
mUsername = view.findViewById(R.id.user_name);
mEmail = view.findViewById(R.id.email);
mPhone = view.findViewById(R.id.phone_number);
mPassword = view.findViewById(R.id.signup_password);
ccp = view.findViewById(R.id.ccp);
mSignUp = (Button) view.findViewById(R.id.signup_btn1);
mSignUp.setOnClickListener(this::createUser);
} catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void createUser(View view) {
try {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
mAuth = FirebaseAuth.getInstance();
mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Toast.makeText(getContext(), "User Created", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Erro I got while performing this functionality in exception:
Attempt to invoke Virtual Method 'void.android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
CodePudding user response:
Why do you not using ViewBinding it will remove the pressure of findViewById()
for more info, you can check view binding setup
And you can also check this How to use ViewBinding in Fragment
According to @VishalBeep, you can do this :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_buyer_fragment, container, false);
mFirstname = view.findViewById(R.id.first_name);
mLastname = view.findViewById(R.id.last_name);
mUsername = view.findViewById(R.id.user_name);
mEmail = view.findViewById(R.id.email);
mPhone = view.findViewById(R.id.phone_number);
mPassword = view.findViewById(R.id.signup_password);
ccp = view.findViewById(R.id.ccp);
mSignUp = (Button) view.findViewById(R.id.signup_btn1);
mSignUp.setOnClickListener(this::createUser);
}
CodePudding user response:
I was doing a very little mistake I was using Tab Layout so I have 2 Fragments open in parallel and I was using the button ID of 2nd Fragment while I working on 1st Fragment. I know that was a stupid mistake but I am new to android development so I think that this can be expected from a beginner. The same code worked for me I have not used any Binding View just change that ID and know code is working fine for me.