为什么FirebaseAuth.getInstance().getCurrentUser()在Android中返回空值 [英] Why FirebaseAuth.getInstance().getCurrentUser() is returning null value in android

查看:108
本文介绍了为什么FirebaseAuth.getInstance().getCurrentUser()在Android中返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注使用Firebase发送图像的代码

我已经配置了firebase存储URL,数据库URL,数据库规则(true),sha1将google-services.json放置在应用程序文件夹中,但是当我调试代码时,it(user)显示空值FirebaseUser user = firebaseAuth.getCurrentUser(); 我的代码如下:

I have configured the firebase storage url,database url ,database rule(true),sha1 placed google-services.json in app folder but when i debug the code the it(user) shows null value FirebaseUser user = firebaseAuth.getCurrentUser(); .My code is given below:

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LoginActivity
    extends AppCompatActivity
    implements FirebaseAuth.AuthStateListener
{

    @Bind( R.id.login_button )
    Button loginButton;

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    @Override
    protected void onCreate( @Nullable Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_login );
        ButterKnife.bind( this );
    }

    @Override
    public void onStart()
    {
        super.onStart();
        firebaseAuth.addAuthStateListener( this );
        verifyCurrentUserAndStartMainActivityIfLoggedIn();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        firebaseAuth.removeAuthStateListener( this );
    }

    public void onLoginClicked( View view )
    {
        loginButton.setEnabled( false );
        firebaseAuth.signInAnonymously();
    }

    @Override
    public void onAuthStateChanged( @NonNull FirebaseAuth firebaseAuth )
    {
        verifyCurrentUserAndStartMainActivityIfLoggedIn();
    }

    private void verifyCurrentUserAndStartMainActivityIfLoggedIn()
    {
        FirebaseUser user = firebaseAuth.getCurrentUser();

        if ( user != null )  //HERE I AM GETTING "user=null" ??
        {

            startActivity( new Intent( this, MainActivity.class ) );
            Toast.makeText(this,"Login success",Toast.LENGTH_LONG).show();
            finish();
        }
        else
        {
            loginButton.setEnabled( true );
        }
    }

}

成功登录后,当(用户!= null)下一个活动显示时,下一个活动的示例URL为

After successfully login when (user !=null) the next activity displays now the sample url of next activity is

//    StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );

并且我已经在firebase中创建了新的存储,其URL为:

and i have created new storage in firebase get the url is:

 StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");

当我使用上述网址时,应用程序崩溃.代码如下:

When I use above url application crashes. The code is given below:

public class MainActivity extends AppCompatActivity
{

    static final int REQUEST_IMAGE_CAPTURE = 1;

    FirebaseDatabase database = FirebaseDatabase.getInstance();

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    DatabaseReference databaseReference = database.getReference( "messages" );

    FirebaseStorage storage = FirebaseStorage.getInstance();

//    StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );
    StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");

我找不到我想念的地方吗?

I am unable to find where I am missing?

推荐答案

您的代码似乎离正式的 firebase文档

Your code seems to be far from oficial firebase documentation

  1. 确保您在Firebase控制台中启用了匿名登录
  2. 使用回调mAuth.signInAnonymously().addOnCompleteListener
  3. 匿名登录 在onComplete()中的
  4. 观察Task.isSuccessful()是否为真,如果不是,则返回task.getException().
  5. 并始终遵循官方文档,而不仅仅是您在github上找到的人
  1. ensure you have anonymous login enabled in firebase console
  2. sign in anonymously with callback mAuth.signInAnonymously().addOnCompleteListener
  3. in onComplete() observe whether Task.isSuccessful() is true and if not what task.getException() return.
  4. and always follow official documentation not just somebody you have found on github

如果您真的想知道为什么FirebaseUsernull的原因,请阅读

If you really want to know why your FirebaseUser is null, then read firebase docs

如果您发布例外情况,我可以为您提供帮助.

If you post the exception I can help you.

这篇关于为什么FirebaseAuth.getInstance().getCurrentUser()在Android中返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆