为了账号安全,请及时绑定邮箱和手机立即绑定

使用 NativeModules 时,在 React Native 中“必须被捕获或声明为抛出”

使用 NativeModules 时,在 React Native 中“必须被捕获或声明为抛出”

忽然笑 2023-09-06 17:00:05
我想构建一个本机模块来对重启设备进行本机反应。我发现这个在java中以编程方式关闭设备。它在 android studio 中单独工作,但是当我尝试在本机模块中使用它时,出现以下错误https://pasteboard.co/IztJ6ll.jpgC:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\java\com\reactnativetest\ToastModule.java:42: error: unreported exception IOException; must be caught or declared to be thrown    Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });                                            ^C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\java\com\reactnativetest\ToastModule.java:43: error: unreported exception InterruptedException; must be caught or declared to be thrown      proc.waitFor();                  ^2 errors> Task :app:compileDebugJavaWithJavac FAILEDFAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation failed; see the compiler error output for details.* Try:Run with --stacktrace option to get the stack trace. Run with --info or --debugoption to get more log output. Run with --scan to get full insights.* Get more help at https://help.gradle.orgBUILD FAILED in 9s57 actionable tasks: 1 executed, 56 up-to-dateerror Could not install the app on the device, read the error above for details.Make sure you have an Android emulator running or a device connected and haveset up your Android development environment:https://facebook.github.io/react-native/docs/getting-started.htmlerror Command failed: gradlew.bat app:installDebug. Run CLI with --verbose flagfor more details.
查看完整描述

1 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

它就在日志中:

C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\java\com\reactnativetest\ToastModule.java:43: 错误:未报告异常 InterruptedException; 必须被捕获或声明抛出 proc.waitFor();

proc.waitFor()抛出异常。您应该用 try-catch 包围它,或者在方法签名中声明throws InterruptedException并在其他地方处理它


查看完整回答
反对 回复 2023-09-06
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信