如何使用FirebaseAuth解决此问题?

时间:2019-06-20 00:39:26

标签: java android firebase firebase-authentication

我想使用firebaseAuth注册一个新用户,但是我一直收到错误消息

我已经使用无效进行调试,然后重新启动并重建了项目,甚至清理了项目,但仍无法成功运行

RegisterActivity

#Program to open provided number of top google search result pages along with the google page

import sys, webbrowser, bs4, requests

if len(sys.argv) > 1:
    number_of_pages = int(sys.argv[1])

if len(sys.argv) > 2:
    search = "/search?q="+'+'.join(sys.argv[2:])
else:
    search=""

webbrowser.open("https://www.google.com"+search)
google_result_page = requests.get("https://www.google.com"+search)
google_soup = bs4.BeautifulSoup(google_result_page.text)

elems = google_soup.select(".r a")

number_of_pages = min(number_of_pages, len(elems))

for i in range(number_of_pages):
    webbrowser.open("http://www.google.com"+elems[i].get('href'))

}

MainActivity

public class RegisterActivity extends AppCompatActivity{

private EditText mNickname;
private EditText mEmail;
private EditText mPassword;
private TextView gotoLoginPage;
private Button mRegisterbutton;
private FirebaseAuth mAuth;
private ProgressDialog mRegProgress;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    mNickname = findViewById(R.id.registerNicknameId);
    mEmail = findViewById(R.id.registerEmailId);
    mPassword = findViewById(R.id.registerPasswordId);
    FirebaseApp.initializeApp(this);


    mRegisterbutton = findViewById(R.id.registerButton);
    gotoLoginPage = findViewById(R.id.gotoLoginPage);

   getSupportActionBar().setTitle("Create new account");
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);

   mRegProgress = new ProgressDialog(this);

    mRegisterbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nickname = mNickname.getText().toString();
            String email = mEmail.getText().toString();
            String password = mPassword.getText().toString();

            if (mNickname.equals("")|| mEmail.equals("") || mPassword.equals("")){
                Toast.makeText(getApplicationContext(), "Fill in the fields", Toast.LENGTH_LONG).show();
                mRegProgress.show();

                registerUser(email, password);
            }
            Intent toHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
            startActivity(toHomePage);
            finish();
        }
    });

    gotoLoginPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent tologinPage = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(tologinPage);
        }
    });

}

private void registerUser(String email, String password) {
    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()){
                Intent gotoHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
                startActivity(gotoHomePage);
                finish();
            }
        }
    });
}

}

这是我运行程序时遇到的错误。尝试在null对象引用上调用虚拟方法'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'

1 个答案:

答案 0 :(得分:0)

错误来自:

public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}

由于您没有在任何地方初始化mAuth,因此它是null.getCurrentUser(),这会引发错误。

mAuth中初始化onCreate

mAuth = FirebaseAuth.getInstance();

或者在使用时将mAuth替换为FirebaseAuth.getInstance()