简体   繁体   English

关闭 inte.net 时检查崩溃 react-native android

[英]Checking crash when internet is turned off react-native android

I've encountered a very funny issue in a react-native app.我在 react-native 应用程序中遇到了一个非常有趣的问题。 I've got a project which is almost fully developed.我有一个几乎完全开发的项目。 Now the thing is, the android app crashes as soon as the inte.net is turned off which is not the case in iOS. This is what I got on Crashlytics for the crash -现在的情况是,一旦关闭 inte.net,android 应用程序就会崩溃,而 iOS 中的情况并非如此。这就是我在 Crashlytics 上看到的崩溃原因 -

Fatal Exception: com.facebook.react.common.JavascriptException: undefined is not a function (evaluating 'n({type:e.connectionType,effectiveType:e.effectiveConnectionType})'), stack:
<unknown>@287:358
value@32:1366
value@18:3177
<unknown>@18:911
value@18:2606
value@18:883

       at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:56)
       at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:40)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:374)
       at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
       at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
       at android.os.Looper.loop(Looper.java:155)
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
       at java.lang.Thread.run(Thread.java:818)

While debugging, I get that red overlay on Android, so can anyone suggest on how to find the root cause of the crash.调试时,我在 Android 上看到红色覆盖,所以任何人都可以建议如何找到崩溃的根本原因。 For starters, I can see that the project has used both对于初学者,我可以看到该项目同时使用了

NetInfo.isConnected.addEventListener(
            'connectionChange',
            this.connectivityChangeListener
        );

at some places and at some places在某些地方和某些地方

let isConnected = await NetInfo.isConnected.fetch();

to detect the.network connection.检测网络连接。

try this way, 尝试这种方式

NetInfo.getConnectionInfo().then((connectionInfo) => {
  console.log('Initial, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
});

function handleFirstConnectivityChange(connectionInfo) {
  console.log('First change, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
  NetInfo.removeEventListener(
    'connectionChange',
    handleFirstConnectivityChange
  );
}

//use it like this way, //以这种方式使用它,

NetInfo.addEventListener(
  'connectionChange',
  handleFirstConnectivityChange
);

You better use useNetInfo() hook that returns global inte.net state.您最好使用返回全局 inte.net state 的 useNetInfo() 挂钩。

import { useNetInfo } from "@react-native-community/netinfo";

export default function MyCommunities() {
  const netInfo = useNetInfo();

 if(netInfo.isConnected){
   //TODO:
 }

 return (<View> </View>);
}
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android上的React-Native Facebook登录崩溃 - React-native facebook login crash on Android 运行发行版时React-native + Firebase崩溃 - React-native + firebase crash when run release version 使用 @react-native-community/blur 和 react-native 0.66.3 在 android 上崩溃 - using @react-native-community/blur with react-native 0.66.3 crash on android 尝试基于 AsyncStorage React-Native 渲染组件时,对象作为反应子对象无效 - objects are not valid as a react child when trying to render component based off of AsyncStorage React-Native React-native 用于 Android 汽车 - React-native for Android Automotive Android中的react-native ActivityIndi​​cator - react-native ActivityIndicator in Android React-Native Android方向 - React-Native Android orientation 在react-native(Android)中解除键盘时取消焦点文本输入 - Unfocus text input when dismissing keyboard in react-native (Android) 运行react-native run-android时,任务&#39;:react-native-icons:compileReleaseAidl&#39;的执行失败 - Execution failed for task ':react-native-icons:compileReleaseAidl' when running react-native run-android 是否可以在 JavaScript 中检测到 Android 和 iOS 浏览器中的屏幕何时关闭 - Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM