首页 > 解决方案 > 使用 NativeModules 时在本机反应中“必须被捕获或声明为被抛出”

问题描述

我想构建一个原生模块来反应原生的重启设备。我在 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.

这是我的本机模块:

package com.reactnativetest;
import android.widget.Toast;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;
import java.util.*;
import java.io.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


public class ToastModule extends ReactContextBaseJavaModule {
  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";
  public ToastModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

@Override
  public String getName() {
    return "ToastExample";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = new HashMap<>();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    return constants;
  }

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
    Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
      proc.waitFor();
  }
}

标签: javareact-native

解决方案


它就在日志中:

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

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


推荐阅读