为什么FirebaseAuth.getInstance().getCurrentUser()在Android中返回空值 [英] Why FirebaseAuth.getInstance().getCurrentUser() is returning null value in 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
- 确保您在Firebase控制台中启用了匿名登录
- 使用回调
mAuth.signInAnonymously().addOnCompleteListener
匿名登录
在 - 观察
Task.isSuccessful()
是否为真,如果不是,则返回task.getException()
. - 并始终遵循官方文档,而不仅仅是您在github上找到的人
onComplete()
中的- ensure you have anonymous login enabled in firebase console
- sign in anonymously with callback
mAuth.signInAnonymously().addOnCompleteListener
- in
onComplete()
observe whetherTask.isSuccessful()
is true and if not whattask.getException()
return. - and always follow official documentation not just somebody you have found on github
如果您真的想知道为什么FirebaseUser
是null
的原因,请阅读
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屋!