2017-12-03 10 views
1

아이브가 firebase를 사용하여 내 앱에 로그인 할 때 사용자 아이디를 찾을 수 없습니다. 하지만 난 구글에 로그인 할 때 그 다음 벌금 로그 i는 사용자 ID를 필요로하고 내가 이메일 다음 기호를 사용하여 로그인하면 그것이Firebase Google에 로그인 할 때

java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference 

또한 내 로그에이 오류를 cliving 찾을 xant 때문에 내 주요 활동에 충돌 Google 계정으로 로그인 할 때 내 비밀번호를 삭제하고 내 계정을 삭제하지 않으면 다시 이메일을 사용하여 인증 할 수 없습니다.

로그인 내 코드는 내가 한 OnCreate

private void GetUser_Firebase() { 
    user = FirebaseAuth.getInstance().getCurrentUser(); 
    userid = user.getUid(); 
    DatabaseReference ref1 = mrootRef.getReference(); 
    mloggedinas = (TextView) findViewById(R.id.tvloggedin); 

    ref1.child("User").child(userid).addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      String m = dataSnapshot.child("screenname").getValue(String.class); 
      if (m == null) { 
       Toast.makeText(MainActivity.this, "Please fill in the above fields", Toast.LENGTH_LONG).show(); 
       startActivity(new Intent(MainActivity.this, Edit_profile.class)); 
      } else { 
       mloggedinas.setText(m); 
      } 
     } 

편집에서이 메소드를 호출하는 사용자 ID를 요구 내 주요 활동에

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //hides keyboard on start; 
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    setContentView(com.example.harrops.h20droidapp2.R.layout.activity_login); 
    Intent intent = new Intent(Login.this, UpdateService.class); 
    startService(intent); 
    mAuth = FirebaseAuth.getInstance(); 
    cbrememberme = (CheckBox) findViewById(R.id.cbrememberme); 
    mgo = (Button) findViewById(R.id.btnsubmit); 
    mgo.setText("Login"); 
    memail = (EditText) findViewById(R.id.etemail); 
    mpass = (EditText) findViewById(R.id.etpass); 
    mforgot = (TextView) findViewById(R.id.tvforgotpass); 
    mreset = (Button) findViewById(R.id.btnreset); 
    mmember = (CheckBox) findViewById(R.id.cbexisiting); 
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
    loginPrefsEditor = loginPreferences.edit(); 

    saveLogin = loginPreferences.getBoolean("saveLogin", false); 
    if (saveLogin == true) { 
     memail.setText(loginPreferences.getString("username", "")); 
     mpass.setText(loginPreferences.getString("password", "")); 
     cbrememberme.setChecked(true); 
     signinnorm(); 
    } 

    mmember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (mmember.isChecked() == false) { 
       mgo.setText("Sign in"); 
       memail.setHint("Sign up with a a valid email address"); 

      } else { 
       mgo.setText("Register"); 
       mpass.setHint("Enter new password"); 
      } 
     } 
    }); 

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(getString(R.string.default_web_client_id)) 
      .requestEmail() 
      .build(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); 

    findViewById(R.id.sign_in_button).setOnClickListener(this); 
    signInButton.setSize(SignInButton.SIZE_STANDARD); 

    mreset.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mreset.setVisibility(View.GONE); 
      mpass.setVisibility(View.VISIBLE); 
      ResetPassword(); 
     } 
    }); 

    mforgot.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mpass.setVisibility(View.GONE); 
      mreset.setVisibility(View.VISIBLE); 


     } 
    }); 
    mgo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (!mmember.isChecked()) { 
       signinnorm(); 

      } else { 
       signup(); 
      } 
     } 
    }); 
} 

private void signup() { 
    String email; 
    String password; 
    email = memail.getText().toString().trim(); 
    password = mpass.getText().toString().trim(); 
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { 

     memail.setHint(getString(R.string.ENTERYOUREMAIL)); 
     memail.setHintTextColor(RED); 

     mpass.setHint(getString(R.string.ENTERYOURPASSWORD)); 
     mpass.setHintTextColor(RED); 
    } else { 


     mAuth.createUserWithEmailAndPassword(email, password) 
       .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, "createUserWithEmail:success"); 
          FirebaseUser user = mAuth.getCurrentUser(); 
          mmember.setChecked(false); 
          cbrememberme.setChecked(true); 
          Toast.makeText(Login.this, "Thanks for signing up. click login in to continue", Toast.LENGTH_SHORT); 

          ; 
         } else { 
          // If sign in fails, display a message to the user. 
          Log.w(TAG, "createUserWithEmail:failure", task.getException()); 
          Toast.makeText(Login.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 

         } 
        } 
       }); 
    } 
} 

private void signinnorm() { 
    String email = memail.getText().toString().trim(); 
    String password = mpass.getText().toString().trim(); 
    if (cbrememberme.isChecked()) { 
     loginPrefsEditor.putBoolean("saveLogin", true); 
     loginPrefsEditor.putString("username", email); 
     loginPrefsEditor.putString("password", password); 
     loginPrefsEditor.commit(); 
    } else { 
     loginPrefsEditor.clear(); 
     loginPrefsEditor.commit(); 

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

     memail.setHint(getString(R.string.ENTERYOUREMAIL)); 
     memail.setHintTextColor(RED); 

     mpass.setHint(getString(R.string.ENTERYOURPASSWORD)); 
     mpass.setHintTextColor(RED); 
    } else { 
     mAuth.signInWithEmailAndPassword(email, password) 
       .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, "signInWithEmail:success"); 
          FirebaseUser user = mAuth.getCurrentUser(); 
          startActivity(new Intent(Login.this, MainActivity.class)); 
          finish(); 

         } else { 
          // If sign in fails, display a message to the user. 
          Log.w(TAG, "signInWithEmail:failure", task.getException()); 
          Toast.makeText(Login.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
    } 
} 

private void ResetPassword() { 
    FirebaseAuth auth = FirebaseAuth.getInstance(); 
    String emailAddress; 
    emailAddress = memail.getText().toString().trim(); 

    auth.sendPasswordResetEmail(emailAddress) 
      .addOnCompleteListener(new OnCompleteListener<Void>() { 
       @Override 
       public void onComplete(@NonNull Task<Void> task) { 
        if (task.isSuccessful()) { 
         Log.d(TAG, "Email sent."); 
        } 
       } 
      }); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.sign_in_button: 
      signIn(); 
      break; 
    } 
} 

private void signIn() { 
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
    startActivityForResult(signInIntent, RC_SIGN_IN); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
    } 
} 

private void handleSignInResult(GoogleSignInResult result) { 

    if (result.isSuccess()) { 
     GoogleSignInAccount account = result.getSignInAccount(); 
     firebaseAuthWithGoogle(account); 
     startActivity(new Intent(Login.this, MainActivity.class)); 


    } else { 
     // Signed out, show unauthenticated UI. 

    } 
} 

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); 

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
    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(); 

        } else { 
         // If sign in fails, display a message to the user. 
         Log.w(TAG, "signInWithCredential:failure", task.getException()); 
         Toast.makeText(Login.this, "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 

        } 

        // ... 
       } 
      }); 
} 

다음입니다 ****** 내가 이것을 사용할 수 메소드 대신 로그인 활동을 사용합니까? 이것은 firebase 웹 사이트에서 왔습니까?

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); 

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
    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(GoogleSignInActivity.this, "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 
         updateUI(null); 
        } 

        // ... 
       } 
      }); 
} 

답변

0

은 내 주요 활동보다는 가입시 get user id 방법을 호출하여 문제가 해결되는 문제를 해결했습니다.

내 Google 인증 방법에 넣습니다. 액세스