使用Firebase的GoogleApiClient中的NullPointerException异常 [英] Rare NullPointerException in GoogleApiClient using Firebase

查看:284
本文介绍了使用Firebase的GoogleApiClient中的NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GoogleApiClient通过他们的Google帐户登录用户。基本上,我使用的是与Google登录的Firebase身份验证服务。 $ b

<但是我每天都会在某些设备上发生这种崩溃。当我测试我自己的一些设备(OnePlus 3,Nexus 5X,Moto G等)时,我从来没有看到这个崩溃。

 致命异常:java.lang.NullPointerException:尝试在com.google的空对象引用
上调用虚方法java.lang.String java.lang.String.toLowerCase(java.util.Locale)'。 android.gms.internal.zzamy.zzd(未知来源)
在com.google.android.gms.internal.zzamv.n(未知来源)
来自com.google.android.gms.internal。 zzamv.zza(未知源)
在com.google.android.gms.internal.zzamv $ 2.run(未知源)
在java.lang.Thread.run(Thread.java:818)

这是我正在做的实例化GoogleApiClient。在正常情况下,我没有看到崩溃,但在某些设备中,我看到了这一点。这是我在代码中所做的,

pre $ String $ mClientId = parcel.getProviderExtra()。getString(CLIENT_ID_KEY);
GoogleSignInOptions googleSignInOptions;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(mClientId)
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(App.getContext())
.addApi(Auth.GOOGLE_SIGN_IN_API,googleSignInOptions)
.build();

mGoogleApiClient.connect();

我在这里做错了什么?我最好的选择就是 App.getContext(),它给了应用程序上下文来实例化客户端。这可能是问题吗?

我可以使用活动上下文,但使用会导致内存泄漏。这里有什么问题,怎么解决?



这对一些刚刚打开应用程序并尝试唱歌时发生崩溃的用户创造了非常糟糕的体验

解决方案

你好你可以试试这个代码这是我工程中的一个工作代码
首先初始化google api client在创建

  public void initGPlus(){
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this / * FragmentActivity * /,this / * OnConnectionFailedListener * /)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
}

并在按钮上点击调用另一个函数

 意图signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent,RC_SIGN_IN);

在On活动结果中得到了你的回应并处理了一个函数结果

  if(requestCode == RC_SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);



$ b $ p
$ p

在你的活动中实现这个接口

 实现GoogleApiClient.OnConnectionFailedListener 


I am using GoogleApiClient to login users using their Google account. Basically, I am using Firebase Auth with Google sign in.

But I am getting this crash on some devices every single day. When I test on some of my own devices (OnePlus 3, Nexus 5X, Moto G, etc) I never see this crash.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference
       at com.google.android.gms.internal.zzamy.zzd(Unknown Source)
       at com.google.android.gms.internal.zzamv.n(Unknown Source)
       at com.google.android.gms.internal.zzamv.zza(Unknown Source)
       at com.google.android.gms.internal.zzamv$2.run(Unknown Source)
       at java.lang.Thread.run(Thread.java:818)

This is what I am doing to instantiate the GoogleApiClient. I don't see the crash in normal situations but in some devices I am seeing this. Here is what I am doing in code,

String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
GoogleSignInOptions googleSignInOptions;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(mClientId)
                .requestEmail()
                .build();

mGoogleApiClient = new GoogleApiClient.Builder(App.getContext())
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

mGoogleApiClient.connect();

What am I doing wrong here? My best bet is, App.getContext() which gives the application context to instantiate the client. Can this be the problem?

I can use the activity context, but using that leads to a memory leak. What is the problem here and how can it be solved?

This is creating a very poor experience to some users who get a crash just after opening the app and trying to sing in.

解决方案

Hello You can try this code this is a working code in my project first initialize google api client in on create

public void initGPlus() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

and on the button click call another function

 Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

In On Activity result just got your response and handle that result in a function

if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }

implement this interface in your activity

 implements GoogleApiClient.OnConnectionFailedListener

这篇关于使用Firebase的GoogleApiClient中的NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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