简体   繁体   中英

Registration page while using Firebase in Android Studio is not working

I'm trying to make registration while using google firebase but it is not working. The whole activity is irresponsive to the code. I have added few show errors for checking the email and password fields, that also not working. I can type in the EditText fields but when I click register nothing happens. Not even the errors are showing. I am a beginner to android development. What should I do to make it work?

Here is my MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editTextrollno, editTextuid;
    private MaterialButton login;
    private FirebaseAuth mAuth;
    private TextView register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.register:
                startActivity(new Intent(this, RegisterUser.class));
                break;
        }
    }
}

Here is my RegisterUser.java

public class RegisterUser extends AppCompatActivity implements 
    View.OnClickListener { 
    private FirebaseAuth mAuth;
    private EditText editTextEmail, editTextUid;
    private TextView signup, register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);

        mAuth = FirebaseAuth.getInstance();

        signup = (TextView) findViewById(R.id.signup);
        signup.setOnClickListener(this);

        register = (Button) findViewById(R.id.signupbtn);
        register.setOnClickListener(this);

        editTextEmail = (EditText) findViewById(R.id.email);
        editTextUid = (EditText) findViewById(R.id.uid);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.signup:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.register:
                register();
                break;
        }
    }

    private void register() {
        String email = editTextEmail.getText().toString().trim();
        String uid = editTextUid.getText().toString().trim();

        if(email.isEmpty()){
            editTextEmail.setError("Email is required");
            editTextEmail.requestFocus();
            return;
        }

        if(uid.isEmpty()){
            editTextUid.setError("UID is required");
            editTextUid.requestFocus();
            return;
        }

        if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            editTextEmail.setError("Please provide valid Email");
            editTextEmail.requestFocus();
            return;
        }

        if(uid.length() < 9){
            editTextUid.setError("Min UID length should be 9 characters");
            editTextUid.requestFocus();
            return;
        }

        mAuth.createUserWithEmailAndPassword(email, uid)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful()){
                            User user = new User(email);

                            FirebaseDatabase.getInstance().getReference("Users")
                                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                    .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if(task.isSuccessful()){
                                        Toast.makeText(RegisterUser.this, "Student has been registered", Toast.LENGTH_LONG).show();

                                    }else{
                                        Toast.makeText(RegisterUser.this, "Failed to register", Toast.LENGTH_LONG).show();
                                    }
                                }
                            });



                        }else{
                            Toast.makeText(RegisterUser.this, "Failed to register", Toast.LENGTH_LONG).show();
                        }
                    }
                });

        }
    }

Here is my gradle dependencies

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:20.0.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.android.volley:volley:1.1.1'

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LoginApp">
        <activity
            android:name=".RegisterUser"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

You need to make sure everything is setup correctly with Firebase and your application.

Please follow the link and check every steps is finished. https://firebase.google.com/docs/auth/android/start

Please update your dependencies according to documentation. 在此处输入图像描述

Hope your problem will be fixed soon.

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