2017-12-03 1 views
1

ive intergrated google Anmelden in meiner App mit Firebase. aber wenn ich mit Google melden Sie sich an loggt es in feinen dann stürzt auf meine Haupttätigkeit, weil ich die Benutzer-ID benötigen und es xant es finden cliving diesen Fehler in meinem LogFirebase Benutzer-ID nicht finden, wenn Sie sich mit Google anmelden

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

auch wenn ich mit E-Mail dann Zeichen Anmelden Mit einem Google-Konto löscht es mein Passwort aus der E-Mail-Anmeldung und lässt mich nicht mehr mit meiner E-Mail authentifizieren, es sei denn, ich lösche mein Konto.

für die Anmeldung ist mein Code

@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(); 

        } 

        // ... 
       } 
      }); 
} 

dann in meiner Haupttätigkeit nenne ich für Benutzer-ID dieses Verfahren in 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); 
      } 
     } 

EDIT Aufruf ****** Kann ich diese verwenden Methode für meine Hauptaktivität statt Login-Aktivität? Dies ist von der Firebase-Website?

Antwort

0

behoben das Problem, es zu haben, indem Sie die Get-User-ID-Methode auf Anmeldung anstelle meiner Haupttätigkeit aufrufen.

Ich lege es in die Google Auth-Methode onSuccess

Verwandte Themen