我的应用程序上有一个用react-native编写的后台服务。当启动服务时,然后关闭应用程序并发出警报:“已停止:不幸”。我在android studio上打开项目进行测试和调试。我认为这是问题。

我的本机版本:0.0.1
和我的包json一样;


{
  "name": "kimdeyizApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "lint": "eslint",
    "postinstall": "npx jetify"
  },
  "dependencies": {
    "react": "16.9.0",
    "react-native": "0.61.2",
    "react-native-axios": "^0.17.1",
    "react-native-elements": "^1.2.6",
    "react-native-push-notification": "^3.1.9",
    "react-native-router-flux": "^4.0.6",
    "react-redux": "^7.0.3",
    "redux": "^4.0.1",
    "redux-actions": "^2.6.5",
    "socket.io-client": "^2.3.0",
    "watchman": "^1.0.0"
  },
  "devDependencies": {
    "@babel/core": "7.6.4",
    "@babel/runtime": "7.6.3",
    "@react-native-community/eslint-config": "0.0.3",
    "babel-jest": "24.9.0",
    "eslint": "6.5.1",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-plugin-import": "^2.17.2",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-react": "^7.13.0",
    "jest": "24.9.0",
    "jetifier": "^1.6.4",
    "metro-react-native-babel-preset": "0.51.1",
    "react-test-renderer": "16.9.0"
  },
  "jest": {
    "preset": "react-native"
  }
}



HeadlessJsTaskConfig.java

package com.facebook.react.jstasks;
import com.facebook.react.bridge.WritableMap;
public class HeadlessJsTaskConfig {
  private final String mTaskKey;
  private final WritableMap mData;
  private final long mTimeout;
  private final boolean mAllowedInForeground;
  private final HeadlessJsTaskRetryPolicy mRetryPolicy;
  public HeadlessJsTaskConfig(String taskKey, WritableMap data) {
    this(taskKey, data, 0, false);
  }
  public HeadlessJsTaskConfig(String taskKey, WritableMap data, long timeout){
    this(taskKey, data, timeout, false);
  }
  public HeadlessJsTaskConfig(
      String taskKey, WritableMap data, long timeout, boolean allowedInForeground) {
    this(taskKey, data, timeout, allowedInForeground, NoRetryPolicy.INSTANCE);
  }
  public HeadlessJsTaskConfig(
      String taskKey,
      WritableMap data,
      long timeout,
      boolean allowedInForeground,
      HeadlessJsTaskRetryPolicy retryPolicy) {
    mTaskKey = taskKey;
    mData = data;
    mTimeout = timeout;
    mAllowedInForeground = allowedInForeground;
    mRetryPolicy = retryPolicy;
  }

  public HeadlessJsTaskConfig(HeadlessJsTaskConfig source) {
    mTaskKey = source.mTaskKey;
    mData = source.mData.copy();
    mTimeout = source.mTimeout;
    mAllowedInForeground = source.mAllowedInForeground;

    final HeadlessJsTaskRetryPolicy retryPolicy = source.mRetryPolicy;
    if (retryPolicy != null) {
      mRetryPolicy = retryPolicy.copy();
    } else {
      mRetryPolicy = null;
    }
  }

  String getTaskKey() {
    return mTaskKey;
  }

  WritableMap getData() {
    return mData;
  }

  long getTimeout() {
    return mTimeout;
  }

  boolean isAllowedInForeground() {
    return mAllowedInForeground;
  }

   HeadlessJsTaskRetryPolicy getRetryPolicy() {
    return mRetryPolicy;
  }

}


和我的错误;

  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kimdeyizapp, PID: 8514
    java.lang.NullPointerException: Attempt to invoke interface method 'com.facebook.react.bridge.WritableMap com.facebook.react.bridge.WritableMap.copy()' on a null object reference
        at com.facebook.react.jstasks.HeadlessJsTaskConfig.<init>(HeadlessJsTaskConfig.java:88)
        at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:106)
        at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:84)
        at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:123)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 8514 SIG: 9
Process 8514 terminated.


所以其他问题就是这样。

E/cr_VariationsUtils: Failed reading seed file "/data/user/0/com.kimdeyizapp/app_webview/variations_seed": /data/user/0/com.kimdeyizapp/app_webview/variations_seed (No such file or directory)


感谢您的帮助。

最佳答案

HeadlessJsTaskConfig不再将第二个参数“数据”作为null接受,必须传递WritableMap类型的对象。

添加import com.facebook.react.bridge.WritableMap;

并尝试以下代码片段

@Override
protected ​@Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
   ​Bundle extras = intent.getExtras();
   ​WritableMap data = extras != null ? Arguments.fromBundle(extras): Arguments.createMap();

   ​return new HeadlessJsTaskConfig(
           ​"BackgroundTask",
           ​data,
           5000
   ​);
}

关于java - com.facebook.react.bridge.WritableMap.copy()对空对象的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58644393/

10-12 14:04