当前位置: 首页 > 知识库问答 >
问题:

尝试调用虚拟方法“null对象引用”

井洲
2023-03-14

每当我尝试使用firebase auth注册用户时,我的应用程序就会崩溃。我的代码如下:

public class MainActivity extends AppCompatActivity {


   private EditText mEmailField;
   private EditText mPasswordField;

   private Button mLoginBtn;

   private FirebaseAuth mAuth;

   private FirebaseAuth.AuthStateListener mAuthListener;


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

        mAuth = FirebaseAuth.getInstance();

        mEmailField = (EditText)findViewById(R.id.emailField);
        mPasswordField = (EditText)findViewById(R.id.passwordField);

        mLoginBtn = (Button)findViewById(R.id.buttonLogin);

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                if(firebaseAuth.getCurrentUser() != null) {

                    startActivity(new Intent(MainActivity.this, Home.class));

                }

            }
        };

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

                startSignIn();

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);

    }

    private void startSignIn() {

        String email = mEmailField.getText().toString();
        String password = mPasswordField.getText().toString();

        if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

            Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();

        } else {

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

                    if (!task.isSuccessful()) {

                        Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
                    }

                }
            });
        }


    }
}

共有1个答案

秋建义
2023-03-14

在onComplete方法中使用以下代码。

new android.os.Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
        }
    }, 100);

更新

private void startSignIn() {

    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();

    if( email == null || password == null ||  TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

        Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();

    } else {

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

                if (!task.isSuccessful()) {

                    new android.os.Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
    }
}, 100);
                }

            }
        });
    }


}
 类似资料: