首页 > 解决方案 > Android Firebase 用户创建

问题描述

在我的注册活动中,我无法注册到 firebase 并移至下一个活动。我检查了控制台,可以从 Logcat 中看到该帐户也不存在 - 这并不是特别有用。任何帮助将不胜感激。

下面是signup_activity

公共类 SignUpActivity 扩展 AppCompatActivity {

LinearLayout facebookLinearLayout, twitterLinearLayout, googlePlusLinearLayout;
Button registerButton;
ImageView bgImageView;
Button forgotButton;
EditText mUsername, mEmail, mPassword;
FirebaseAuth fAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    initUI();
    initDataBindings();
    initActions();

    registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

//region Init Functions
private void initUI() {

    forgotButton = findViewById(R.id.forgotButton);

    facebookLinearLayout = findViewById(R.id.facebookLinearLayout);
    twitterLinearLayout = findViewById(R.id.twitterLinearLayout);
    googlePlusLinearLayout = findViewById(R.id.googlePlusLinearLayout);
    registerButton = findViewById(R.id.registerButton);
    bgImageView = findViewById(R.id.bgImageView);
    mUsername = findViewById(R.id.username);
    mEmail = findViewById(R.id.email);
    mPassword = findViewById(R.id.password);
    fAuth = FirebaseAuth.getInstance();

}

private void initDataBindings() {
    int id = R.drawable.lightbg;
    Utils.setImageToImageView(getApplicationContext(), bgImageView, id);
}

private void initActions() {
    forgotButton.setOnClickListener(view -> {
        Toast.makeText(getApplicationContext(), "Clicked Forgot Password", Toast.LENGTH_SHORT).show();
    });

    facebookLinearLayout.setOnClickListener(view -> {
        Toast.makeText(getApplicationContext(), "Clicked Facebook - Not available just yet", Toast.LENGTH_SHORT).show();
    });

    twitterLinearLayout.setOnClickListener(view -> {
        Toast.makeText(getApplicationContext(), "Clicked Twitter - Not available just yet", Toast.LENGTH_SHORT).show();
    });

    googlePlusLinearLayout.setOnClickListener(view -> {
        Toast.makeText(getApplicationContext(), "Clicked Google Plus - Not available just yet", Toast.LENGTH_SHORT).show();
    });

    //assign database instances
    fAuth = FirebaseAuth.getInstance();
    mAuthStateListener = new FirebaseAuth.AuthStateListener(){
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null) {
            }
            else{
                startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
            }
        }
    };

    registerButton.setOnClickListener(view -> {
        String email = mEmail.getText().toString().trim();
        String password = mPassword.getText().toString().trim();
        String username = mUsername.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            mUsername.setError("Please provide a Username");
        }
        if (TextUtils.isEmpty(email)) {
            mEmail.setError("Email Required");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mPassword.setError("Password Required");
            return;
        }
        if (password.length() < 6) {
            mPassword.setError("Password must be greater than 6 characters");
            return;
        }
        //Register the user
        fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {
                    Toast.makeText(SignUpActivity.this, "User Created", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(), FeatureDashboardNewsDashboard1Activity.class));
                } else {
                    Toast.makeText(SignUpActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    });
    //endregion
}

并且活动应该在 Firebase 中注册并移动到仪表板

公共类 FeatureDashboardNewsDashboard1Activity 扩展 AppCompatActivity {

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

    initData();

    initUI();

    initDataBinding();

    initActions();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
    }else {
        Toast.makeText(this, "Clicked "+ item.getTitle() , Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}

private void initData() {

}

private void initUI() {

    // Init Toolbar
    initToolbar();

    BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);

    bottomNavigationView.setOnNavigationItemSelectedListener(item -> {

        switch (item.getItemId()) {
            case R.id.homeMenu:
                loadFragment(new FeatureDashboardNewsDashboard1Fragment());
                break;
        }

        return true;
    });

    loadFragment(new FeatureDashboardNewsDashboard1Fragment());

}

private void initDataBinding() {

}

private void initActions() {

}

private void initToolbar() {

    Toolbar toolbar = findViewById(R.id.toolbar);

    toolbar.setNavigationIcon(R.drawable.baseline_menu_black_24);

    if (toolbar.getNavigationIcon() != null) {
        toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.md_white_1000), PorterDuff.Mode.SRC_ATOP);
    }

    toolbar.setTitle("News Dashboard 1");

    try {
        toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    } catch (Exception e) {
        Log.e("TEAMPS", "Can't set color.");
    }

    try {
        setSupportActionBar(toolbar);
    } catch (Exception e) {
        Log.e("TEAMPS", "Error in set support action bar.");
    }

    try {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    } catch (Exception e) {
        Log.e("TEAMPS", "Error in set display home as up enabled.");
    }

}

private void loadFragment(FeatureDashboardNewsDashboard1Fragment fragment) {
    this.getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commitAllowingStateLoss();
}

}

标签: androidfirebaseandroid-activityfirebase-authentication

解决方案


您已经设置了onclickListener两次registerButton,其中一次为空。

删除第一个onclickListener(在您的 oncreate 中),我认为它会解决问题。


推荐阅读