Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Firebase读取用户数据时出现NullPointerException_Java_Android_Firebase_Firebase Authentication - Fatal编程技术网

Java 从Firebase读取用户数据时出现NullPointerException

Java 从Firebase读取用户数据时出现NullPointerException,java,android,firebase,firebase-authentication,Java,Android,Firebase,Firebase Authentication,我创建了一个员工考勤跟踪器应用程序,在模拟过程中发现了一些问题。所以请帮帮我 public class LoginActivity extends Activity { EditText userEmail,userPass; Button loginButton,signupButton,forgotPasswordButton,phoneVerifyButton; private FirebaseAuth mAuth; private static final

我创建了一个员工考勤跟踪器应用程序,在模拟过程中发现了一些问题。所以请帮帮我

public class LoginActivity extends Activity {
    EditText userEmail,userPass;
    Button loginButton,signupButton,forgotPasswordButton,phoneVerifyButton;
    private FirebaseAuth mAuth;
    private static final String TAG = "LoginActivity";
 
 
    private Toolbar toolbar;
 
    public CallbackManager mCallbackManager;
    SharedPreferences pref;
    SharedPreferences.Editor editor;
 
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference(Constants.STUDENT);
 
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setActionBar(toolbar);
        userEmail = findViewById(R.id.log_username);
        userPass = findViewById(R.id.log_password);
        loginButton = findViewById(R.id.loginuser);
        signupButton = findViewById(R.id.signupuser);
        forgotPasswordButton = findViewById(R.id.forgotpassword);
 
        phoneVerifyButton = findViewById(R.id.log_phoneverify);
        mAuth = FirebaseAuth.getInstance();
 
        pref = getSharedPreferences("prefs",MODE_PRIVATE);
        editor = pref.edit();
 
 
 
 
 
 
        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,SignUpActivity.class);
                startActivity(intent);
            }
        });
 
 
 
        forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,ForgotActivity.class);
                startActivity(intent);
            }
        });
 
 
 
        phoneVerifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,PhoneVerifyActivity.class);
                startActivity(intent);
            }
        });
 
 
 
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            String email,pass;
            email = userEmail.getText().toString();
            pass = userPass.getText().toString();
                if(TextUtils.isEmpty(email)){
                    userEmail.setError("Please enter Email");
                    return;
                }
 
                if(TextUtils.isEmpty(pass)){
                    userPass.setError("Please enter Password");
                    return;
                }
 
                mAuth.signInWithEmailAndPassword(email, pass)
                        .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    // Sign in success, update UI with the signed-in user's information
                                    Log.d(TAG, "signInWithEmail:success");
                                    FirebaseUser user = mAuth.getCurrentUser();
                                    Toast.makeText(LoginActivity.this, "Welcome : "+user.getEmail(), Toast.LENGTH_SHORT).show();
                                    updateUI(user);
                                } else {
                                    // If sign in fails, display a message to the user.
                                    Log.w(TAG, "signInWithEmail:failure", task.getException());
                                    Toast.makeText(LoginActivity.this, "Authentication failed.",Toast.LENGTH_SHORT).show();
                                    updateUI(null);
 
                                }
 
                                // ...
                            }
                        });
            }
        });
    }
 
 
 
 
    //public void login(View view){
    //
    //        String email,pass;
    //        email = e1.getText().toString();
    //        pass = e2.getText().toString();
    //        if(TextUtils.isEmpty(email)){
    //            e1.setError("Please enter email");
    //            return;
    //        }
    //        if(TextUtils.isEmpty(pass)){
    //            e2.setError("Please enter pass");
    //            return;
    //        }
    //        fireLogin(email,pass);
    //    }
 
 
    public  void onfblogin(View view) {
        mCallbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = findViewById(R.id.login_button);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "facebook:onSuccess:" + loginResult);
                handleFacebookAccessToken(loginResult.getAccessToken());
            }
 
            @Override
            public void onCancel() {
                Log.d(TAG, "facebook:onCancel");
                // ...
            }
 
            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "facebook:onError", error);
                // ...
            }
        });
// ...
 
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        // Pass the activity result back to the Facebook SDK
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
 
    private void handleFacebookAccessToken(AccessToken token) {
        Log.d(TAG, "handleFacebookAccessToken:" + token);
 
        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }
 
                        // ...
                    }
                });
    }
 
    private void getUserData(FirebaseUser user){
        myRef.child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Student s = dataSnapshot.getValue(Student.class);
                editor.putString(Constants.NAME,s.getName());
                editor.putString(Constants.EMAIL,s.getEmail());
                editor.putString(Constants.COURSE,s.getCourse());
                editor.commit();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
 
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(LoginActivity.this, "Student details error", Toast.LENGTH_SHORT).show();
            }
        });
    }
 
 
    private void updateUI(FirebaseUser user){
        if(user!=null){
            getUserData(user);
 
        }
        //else{
          //  Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            //startActivity(intent);
        //}
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        updateUI(currentUser);
    }
//public void singUp(View view){
    //        Intent intent = new Intent(LoginActivity.this,SignUpActivity.class);
    //        startActivity(intent);
    //    }
}
公共类LoginActivity扩展活动{
EditText userEmail、userPass;
按钮登录按钮、注册按钮、放弃密码按钮、电话验证按钮;
私人消防队;
私有静态最终字符串TAG=“LoginActivity”;
专用工具栏;
公共CallbackManager mCallbackManager;
共享参考优先;
SharedReferences.Editor;
FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference(Constants.STUDENT);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
设置操作栏(工具栏);
userEmail=findviewbyd(R.id.log\u用户名);
userPass=findviewbyd(R.id.log\u密码);
loginButton=findViewById(R.id.loginuser);
signupButton=findviewbyd(R.id.signupuser);
forgotPasswordButton=findViewById(R.id.forgotpassword);
phoneVerifyButton=findViewById(R.id.log\u phoneverify);
mAuth=FirebaseAuth.getInstance();
pref=GetSharedReferences(“prefs”,模式为私有);
编辑器=pref.edit();
signupButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(LoginActivity.this,SignUpActivity.class);
星触觉(意向);
}
});
forgotPasswordButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(LoginActivity.this,forgoActivity.class);
星触觉(意向);
}
});
phoneVerifyButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(LoginActivity.this,PhoneVerifyActivity.class);
星触觉(意向);
}
});
loginButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串电子邮件,传递;
email=userEmail.getText().toString();
pass=userPass.getText().toString();
如果(TextUtils.isEmpty(电子邮件)){
userEmail.setError(“请输入电子邮件”);
返回;
}
if(TextUtils.isEmpty(pass)){
userPass.setError(“请输入密码”);
返回;
}
mAuth.使用电子邮件和密码登录(电子邮件,通行证)
.addOnCompleteListener(LoginActivity.this,新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
Log.d(标记“signInWithEmail:success”);
FirebaseUser=mAuth.getCurrentUser();
Toast.makeText(LoginActivity.this,“欢迎:+user.getEmail(),Toast.LENGTH\u SHORT).show();
updateUI(用户);
}否则{
//如果登录失败,则向用户显示消息。
w(标记“signInWithEmail:failure”,task.getException());
Toast.makeText(LoginActivity.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
updateUI(null);
}
// ...
}
});
}
});
}
//公共无效登录(查看){
//
//字符串电子邮件,传递;
//email=e1.getText().toString();
//pass=e2.getText().toString();
//如果(TextUtils.isEmpty(电子邮件)){
//e1.设置错误(“请输入电子邮件”);
//返回;
//        }
//if(TextUtils.isEmpty(pass)){
//e2.设置错误(“请输入密码”);
//返回;
//        }
//fireLogin(电子邮件、通行证);
//    }
公用void onfblogin(视图){
mCallbackManager=CallbackManager.Factory.create();
LoginButton LoginButton=findViewById(R.id.login_按钮);
setReadPermissions(“电子邮件”、“公共档案”);
registerCallback(mCallbackManager,newfacebookcallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
Log.d(标签“facebook:onSuccess:+loginResult”);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@凌驾
公开作废{
Log.d(标签“facebook:onCancel”);
// ...
}
@凌驾
public void onError(facebook异常错误){
Log.d(标签“facebook:onError”,错误);
// ...
}
});
// ...
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//将活动结果传回Facebook SDK
onActivityResult(请求代码、结果代码、数据);
}
私有无效handleFacebookAccessToken(AccessToken令牌){
日志
private void getUserData(FirebaseUser user){
    myRef.child(user.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Student s = dataSnapshot.getValue(Student.class);
            if(s!=null) {
                editor.putString(Constants.NAME,s.getName());
                editor.putString(Constants.EMAIL,s.getEmail());
                editor.putString(Constants.COURSE,s.getCourse());
                editor.commit();
            }
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }