根据 Firebase 支持页面,我在提交官方错误之前在此发布。希望 Firebase 团队的有人能够提供帮助。
我的 Android 应用程序使用 Firebase 匿名身份验证。我一直在使用模拟器对旧版 Android 版本进行一些测试,并且在 API 15 和 16 上始终出现以下异常(到目前为止...还有更多测试要做):
Caused by: java.lang.NullPointerException
at com.google.android.gms.internal.zzdtp.zzb(Unknown Source)
at com.google.android.gms.internal.zzdtw.zza(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInAnonymously(Unknown Source)
火力基地入门 https://firebase.google.com/docs/android/setup指南将 Firebase 列为支持 Android v4.0 (API 15) 及更高版本,但我想知道这是否已经改变。
我在较新的版本上没有收到此错误。我检查了 API 22 及更高版本,没有遇到任何问题。我将在完成 17 到 21 的测试后报告。到目前为止,15 和 16 肯定会抛出错误。
我的实现很简单,并且与 Firebase 文档一致。
public abstract class BaseActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseUser mUser = null;
private boolean mFirstAuthListenerRun = true;
private FirebaseAuth.AuthStateListener mAuthStateListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We want to know if anyone is signed in, so lets listen for that. Per the docs,
// onCreate is a good place to create the listener:
// Remember though, the event listener can get fired a lot:
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
mFirstAuthListenerRun = false;
// User is signed in
Log.d(GetTag(), "onAuthStateChanged:signed_in:" + user.getUid());
mUser = user;
} else {
// User is signed out
Log.d(GetTag(), "onAuthStateChanged:signed_out");
mUser = null;
if(mFirstAuthListenerRun){
// We're here because the onAuthStateChanged listener has just been registered, but there wasn't a user yet.
// Let's try to sign in.
mFirstAuthListenerRun = false;
Log.d(GetTag(), "onAuthStateChanged:Attempting SignIn");
SignIn();
}
}
AuthStateChanged();
}
};
}
@Override
protected void onStart()
{
super.onStart();
mAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onStop()
{
super.onStop();
mUser = null;
if (mAuthStateListener != null) {
mAuth.removeAuthStateListener(mAuthStateListener);
}
}
public Task<AuthResult> Bounce()
{
SignOut();
return SignIn();
}
public Task<AuthResult> SignIn()
{
return mAuth.signInAnonymously();
}
public void SignOut()
{
mAuth.signOut();
}
public FirebaseUser GetCurrentUser()
{
return mUser;
}
public abstract String GetTag();
public abstract void AuthStateChanged();
}
我已经调试过了,不,mAuth 肯定不为空,如上面的调用跟踪所示。该异常显然来自 Firebase 代码内部,由于混淆,我无法确定问题的根源。
也许这是由于模拟设备上的 Play 服务版本无效造成的?我已经更新了我的图像,并且使用了 Google API 图像,但我知道这些图像并不总是保持最新。
两台设备(API 15 和 16)都运行 Play Services v9.2.56。
我正在编译到 API 27,并编译播放服务依赖项的 v11.6.2:
implementation 'com.google.android.gms:play-services-maps:11.6.2'
implementation 'com.google.android.gms:play-services-places:11.6.2'
implementation 'com.google.android.gms:play-services-identity:11.6.2'
implementation 'com.google.android.gms:play-services-location:11.6.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'com.google.firebase:firebase-core:11.6.2'
implementation 'com.google.firebase:firebase-database:11.6.2'
implementation 'com.google.firebase:firebase-auth:11.6.2'
任何想法将不胜感激。如果没有 Firebase Auth,我的应用程序将完全无用,因此如果 Firebase Auth 不再支持旧版本的 Android,我将不得不升级我的 minSDKTarget。
我在运行 Android 6 及更高版本的许多物理设备上都没有遇到任何问题。
Thanks!