代码之家  ›  专栏  ›  技术社区  ›  Priyanshu kuntal

不变冲突:TurboModuleRegistry.getEnforceing(…):“RNGoogleMobileAdsModule”,在集成admob时处于react本机状态

  •  0
  • Priyanshu kuntal  · 技术社区  · 2 年前

    我是react native的新手,我想将我的应用程序货币化,所以我将Admob集成到react natives中,我使用 npx create-react-native-app 命令,我试着看youtube,谷歌,chatgpt,bard,但什么都不起作用,这是我最后的希望,请帮我这里是我的代码,

    app.json

    enter image description here

    Copybase.js——我正在测试广告的文件,但最近两天没有显示任何内容

    import { Animated, Dimensions, Image, Pressable, StyleSheet, Text, View } from 'react-native';
    import React, { useRef } from 'react';
    import Header from '../components/Header';
    import { useThContext } from '../reducon/context';
    import { Linking } from 'react-native';
    import { BannerAd, BannerAdSize, TestIds } from 'react-native-google-mobile-ads';
    
    const adUnitId = __DEV__ ? TestIds.BANNER : 'ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx';
    
    export default Copybase = ({ navigation }) => {
    
      const animation = useRef(new Animated.Value(1)).current;
      const bgColor = useRef(new Animated.Value(0)).current;
    
    
      const handlePressIn = () => {
        Animated.spring(animation, {
          toValue: 0.97,
          duration: 200,
          useNativeDriver: true,
        }).start();
        Animated.timing(bgColor, {
          toValue: 1,
          duration: 200,
          useNativeDriver: true,
        }).start();
      }
      const handlePressOut = () => {
        Animated.spring(animation, {
          toValue: 1,
          duration: 200,
          useNativeDriver: true,
        }).start();
        Animated.timing(bgColor, {
          toValue: 0,
          duration: 200,
          useNativeDriver: true,
        }).start();
        Linking.openURL(info.link);
      }
    
    
      const { info } = useThContext();
    
      const backgroundColor = bgColor.interpolate({
        inputRange: [0, 1],
        outputRange: ['#749b38', '#648630'],
      })
    
      return (
        <View>
          <Header navi={navigation} text={'BASE LAYOUT'} BackButton={true} />
          <View style={styles.container}>
            <Image style={styles.image} source={info.image} />
            <Pressable onPressIn={() => { handlePressIn() }} onPressOut={() => { handlePressOut() }}>
              <Animated.View style={[styles.copyButton, { transform: [{ scale: animation }], backgroundColor: backgroundColor }]}>
                <Text style={styles.copyButtonText}>COPY LAYOUT</Text>
              </Animated.View>
            </Pressable>
          </View>
          <BannerAd
            unitId={TestIds.BANNER}
            size={BannerAdSize.BANNER}
            requestOptions={{
              requestNonPersonalizedAdsOnly: true,
            }}
          />
        </View>
      )
    }
    const styles = StyleSheet.create({
      container: {
        height: '100%',
        alignItems: 'center',
        backgroundColor: '#f9f9f9',
      },
      image: {
        width: Dimensions.get('window').width,
        height: 300,
        resizeMode: 'contain',
      },
      copyButton: {
        marginTop: 40,
        backgroundColor: '#749b38',
        padding: 12,
        borderRadius: 3,
        shadowColor: "#000000",
        shadowOffset: {
          width: 0,
          height: 1,
        },
        shadowOpacity: 0.16,
        shadowRadius: 1.51,
        elevation: 6,
      },
      copyButtonText: {
        color: '#fff',
        fontWeight: '700',
        fontSize: 18,
      }
    });
    

    我所面临的错误----

    Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNGoogleMobileAdsModule' could not be found. Verify that a module by this name is registered in the native binary., js engine: hermes
    at node_modules\react-native\Libraries\Core\ExceptionsManager.js:null in reportException
    at node_modules\react-native\Libraries\Core\ExceptionsManager.js:null in handleException
    at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:null in handleError
    at node_modules\expo\build\errors\ExpoErrorManager.js:null in createErrorHandler
    at node_modules\expo\build\errors\ExpoErrorManager.js:null in <anonymous>
    at node_modules\@react-native\js-polyfills\error-guard.js:null in ErrorUtils.applyWithGuard
    at node_modules\metro-runtime\src\polyfills\require.js:null in guardedLoadModule
    at http://192.168.29.76:19000/index.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:null in metroRequire
    at http://192.168.29.76:19000/index.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:null in global
    
    0 回复  |  直到 2 年前
        1
  •  2
  •   Abey Bruck    2 年前

    我遇到了同样的错误,问题是使用 expo go 来运行我的应用程序。你必须做的是在使用构建应用程序之后 eas 生成,您必须从下载已生成的应用程序 eas 然后将其安装在您的设备或模拟器上。然后从vs代码运行您的dev服务器,并运行您下载的应用程序。请参照 this 来自youtube或 here 如果你想进一步了解的话,是来自expo doc的同一个视频。我希望这能有所帮助:)

    完成此修复后,您将遇到另一个错误,需要将已安装的节点版本降级为当前版本 LTS 版本,在回答此问题时为18.17.0。要遵循此修复程序,请查看 this