异常java.lang.StackOverflowError:堆栈大小为8MB

时间:2017-10-04 15:32:50

标签: android firebase

我有这个错误,我无法弄清楚它是什么。我认为这是一个循环,但我找不到它。你能帮我找到并安排吗?虽然我不完全确定,但我认为它在这个屏幕上。 prolema匹配在Firebase Auth中访问google的页面。 谢谢大家

我知道还有其他类似的问题,但我找不到答案

Exception java.lang.StackOverflowError: stack size 8MB com.google.android.gms.internal.zzbtg.zzaz (zzbtg.java)

这是我发现问题的活动

public class GoogleSignIn extends BaseActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    View.OnClickListener {

private static final String TAG = "GoogleActivity";
private static final int RC_SIGN_IN = 9001;

// [START declare_auth]
private FirebaseAuth mAuth;
// [END declare_auth]

// [START declare_auth_listener]
private FirebaseAuth.AuthStateListener mAuthListener;
// [END declare_auth_listener]

private GoogleApiClient mGoogleApiClient;
private DatabaseReference mDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.auth_google_sign_in);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    toolbar.setTitleTextColor(Color.WHITE);
    toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.ic_arrow_back_white_24dp));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //What to do on back clicked
            onBackPressed();
        }
    });

    // Button listeners
    findViewById(R.id.sign_in_button).setOnClickListener(this);

    // [START config_signin]
    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // [END config_signin]

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    // [START initialize_auth]
    mAuth = FirebaseAuth.getInstance();
    // [END initialize_auth]

    // [START auth_state_listener]
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // [START_EXCLUDE]

            // [END_EXCLUDE]
        }
    };
    // [END auth_state_listener]
}

// [START on_start_add_listener]
@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}
// [END on_start_add_listener]

// [START on_stop_remove_listener]
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}
// [END on_stop_remove_listener]

// [START onactivityresult]
@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);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            Toast.makeText(GoogleSignIn.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();

        }
    }
}
// [END onactivityresult]

// [START auth_with_google]
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    // [START_EXCLUDE silent]
    showProgressDialog();
    // [END_EXCLUDE]

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {




                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Toast.makeText(GoogleSignIn.this, "Authentication failed", Toast.LENGTH_SHORT).show();
                    }
                    // [START_EXCLUDE]
                    hideProgressDialog();
                    onAuthSuccess(task.getResult().getUser());
                    // [END_EXCLUDE]

                    Intent intent = new Intent(GoogleSignIn.this, HomeActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }
            });
}
// [END auth_with_google]

// [START signin]
public void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
    int i = v.getId();
    if (i == R.id.sign_in_button) {
        signIn();
    }
}
private void onAuthSuccess(FirebaseUser user) {

    SharedPreferences.Editor sharedPreferences = getSharedPreferences("GOOGLE_LOGIN", MODE_PRIVATE).edit();
    sharedPreferences.putString("google", "true").apply();

    mDatabase.child("user-profile").child(user.getUid()).child("username").setValue(user.getDisplayName());
    mDatabase.child("user-profile").child(user.getUid()).child("photoUrl").setValue(user.getPhotoUrl());
    mDatabase.child("user-profile").child(user.getUid()).child("uid").setValue(user.getUid());
    mDatabase.child("user-profile").child(user.getUid()).child("email").setValue(user.getEmail());

}

}

1 个答案:

答案 0 :(得分:5)

问题在于这句话:

mDatabase.child("user-profile").child(user.getUid()).child("photoUrl")
    .setValue(user.getPhotoUrl());

getPhotoUrl()返回Uri。这不是setValue()支持的对象类型之一。您应该存储getPhotoUrl().toString()