You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native集成Firebase谷歌登录触发hashCode空指针报错如何解决

问题根因

  • 核心错误:谷歌登录流程缺失,你未调用react-native-google-signin的授权接口获取用户凭证,直接将空的Providers.google.credential传入signInWithCredential方法,导致原生层解析空字符串时抛出空指针异常。
  • 次要问题1:GoogleSignin.configure配置错误,accountName字段用于指定目标登录账号,你错误填入了谷歌客户端ID,会导致授权流程异常。
  • 次要问题2:依赖冲突,项目同时安装了旧版react-native-firebase v5、新版@react-native-firebase v12、Web版firebase v9三套Firebase SDK,调用逻辑会出现不可预期的冲突。

解决方案

步骤1:补全谷歌登录流程

在LoginScreen组件中新增谷歌登录方法,替换原有错误的点击逻辑:

// 新增谷歌登录方法
const googleSignIn = async () => {
  try {
    // 检查谷歌服务可用性
    await GoogleSignin.hasPlayServices();
    // 唤起谷歌账号授权页,获取用户信息
    const userInfo = await GoogleSignin.signIn();
    // 用拿到的idToken生成Firebase要求的登录凭证
    const googleCredential = Firebase.auth.GoogleAuthProvider.credential(userInfo.idToken);
    // 完成Firebase登录
    await Firebase.auth().signInWithCredential(googleCredential);
    // 登录成功跳转
    navigation.navigate('ProtectedScreen');
  } catch (error) {
    console.error('谷歌登录失败:', error);
    Alert.alert('登录失败', '谷歌授权异常,请稍后重试');
  }
}

修改GoogleSigninButton的onPress属性:

<GoogleSigninButton
  style={loginStyles.googleButton}
  size={GoogleSigninButton.Size.Wide}
  color={GoogleSigninButton.Color.Dark}
  onPress={googleSignIn}
/>

步骤2:修正GoogleSignin配置

删除accountName配置项,修正后代码:

GoogleSignin.configure({
  scopes: ['https://www.googleapis.com/auth/drive.readonly'], // 如无读取谷歌网盘需求可删除该行
  webClientId: Providers.google.PROVIDER_ID,
  offlineAccess: true,
  forceConsentPrompt: true,
});

步骤3:清理冲突依赖

卸载不需要的冗余Firebase SDK:

# npm执行
npm uninstall react-native-firebase firebase
# yarn执行
yarn remove react-native-firebase firebase

@react-native-firebase/app和@react-native-firebase/auth已经是适配React Native的原生SDK,无需额外安装其他版本Firebase包。

步骤4:校验配置参数

确认Providers.google.PROVIDER_ID是从Firebase控制台获取的Web端客户端ID,不能填安卓/iOS端的客户端ID,否则会导致授权失败。

内容的提问来源于stack exchange,提问作者Manuel Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 08:45:01