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

我想构建一个本机模块来对重启设备进行本机反应。我发现这个在java中以编程方式关闭设备。它在 android studio 中单独工作,但是当我尝试在本机模块中使用它时,出现以下错误

https://pasteboard.co/IztJ6ll.jpg

C:\Users\Ali\Desktop\Android\ReactNative\ReactNativeTest\android\app\src\main\ja

va\com\reactnativetest\ToastModule.java:42: error: unreported exception IOExcept

ion; 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\ja

va\com\reactnativetest\ToastModule.java:43: error: unreported exception Interrup

tedException; must be caught or declared to be thrown

      proc.waitFor();

                  ^

2 errors


> Task :app:compileDebugJavaWithJavac FAILED


FAILURE: 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 --debug

option to get more log output. Run with --scan to get full insights.


* Get more help at https://help.gradle.org


BUILD FAILED in 9s

57 actionable tasks: 1 executed, 56 up-to-date

error 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 have

set up your Android development environment:

https://facebook.github.io/react-native/docs/getting-started.html

error Command failed: gradlew.bat app:installDebug. Run CLI with --verbose flag

for more details.


忽然笑
浏览 78回答 1
1回答

慕斯709654

它就在日志中: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并在其他地方处理它
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java