[英]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.