React-Native“无法连接到开发服务器"在 createReactContext() [英] React-Native "Could no connect to development server" in createReactContext()

查看:62
本文介绍了React-Native“无法连接到开发服务器"在 createReactContext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Android react-native 应用因 ExecutionException 而崩溃.

My Android react-native app is crashing with an ExecutionException.

无法连接到开发服务器."

"Could not connect to development server."

崩溃的方法是ReactInstanceManagerImpl.createReactContext().

但这不是正常错误,如此屏幕截图所示:

But this is not the normal error as this screenshot shows:

Logcat 向我展示了这个堆栈跟踪:

Logcat shows me this stacktrace:

Exception in native call from JS
java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException:
 Could not connect to development server.
        at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:860)
        at com.facebook.react.ReactInstanceManagerImpl.access$700(ReactInstanceManagerImpl.java:98)
        at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:194)
        ...
Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException:
        at com.facebook.react.common.futures.SimpleSettableFuture.get(SimpleSettableFuture.java:68)
        at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:831)
        ... 9 more
Caused by: java.lang.RuntimeException:
        at com.facebook.react.bridge.ReactBridge.loadScriptFromFile(Native Method)
        at com.facebook.react.bridge.JSBundleLoader$2.loadScript(JSBundleLoader.java:58)
        at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:148)
        at com.facebook.react.ReactInstanceManagerImpl$4.call(ReactInstanceManagerImpl.java:851)
        ...

这表明服务器已将 javascript 代码发送回设备,并且 ReactBridge.loadScriptFromFile() 不知何故无法处理该包.

This suggests that the server has sent the javascript code back to the device and the ReactBridge.loadScriptFromFile() somehow can't handle the bundle.

要澄清的要点:

  • 这发生在模拟器上设备(通过 USB 连接)
  • 这发生在 Linux (Mint) &视窗 (8.1)
  • 我确实按照正常建议运行了 adb reverse tcp:8081 tcp:8081
  • 我已经运行了 react-native start 并且打包程序/服务器正在运行
  • 设备确实连接到开发服务器,因为这会记录在打包程序窗口中
  • 设备点击的 URL 会返回数据,如果复制到浏览器地址栏中就可以看到
  • this happens on emulator & device (connected via USB)
  • this happens on Linux (Mint) & Windows (8.1)
  • I did run adb reverse tcp:8081 tcp:8081 as per the normal advice
  • I have run react-native start and the packager/server is running
  • the device DOES connect to the development server, as this is logged in the packager window
  • the URL that the device hits does return data, as can be seen if copied into the browser address bar

这与正常无法连接..."消息有何不同?

How is this different from the normal "Could not connect..." message?

以下是服务器未正常运行时发生的情况的屏幕截图:

Here is a screenshot of what happens when the server is legitimately not running:

差异包括:

  • 在错误信息之前没有 ExecutionExceptionRuntimeException
  • 进一步明确提供在服务器上调用的 URL
  • 错误来自DevServerHelper.onFailure()

Logcat 显示堆栈跟踪在这种正常预期情况下完全不同:

Logcat shows the stacktrace to be quite different in this normal expected case:

Unable to download JS bundle
com.facebook.react.devsupport.DebugServerException:
 Could not connect to development server.
        at com.facebook.react.devsupport.DevServerHelper$1.onFailure(DevServerHelper.java:196)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:140)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

推荐答案

这似乎是由于没有正确安装 Windows Build Tools 造成的.这破坏了我的项目所需的 node-gyp 命令.

This seems to have been caused by not having Windows Build Tools correctly installed. This broke the node-gyp commands required by my project.

按照node-gyp 页面上的步骤正确设置了我的机器.

Following the steps on the node-gyp page got my machine set up correctly.

在新构建后,我收到了一次此错误,然后随着项目构建和正常运行而消失.

After a new build I got this error once, and then it disappeared with the project building and running correctly.

这篇关于React-Native“无法连接到开发服务器"在 createReactContext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆