React Native Android Bridge Error: Method addObserver must be called on the main thread

React Native Android Bridge Error: Method addObserver must be called on the main thread

我正在使用 Wootrick SDK 创建一个桥接器来响应本机。当我调用特定函数时,我得到了以下日志,但没有显示 Wootric Survey。

java.lang.IllegalStateException: Method addObserver must be called on the main thread

我的代码如下,

@ReactMethod
public void configureWithClientID(String clientId, String accountToken) {
Activity currentActivity = getCurrentActivity();

if (currentActivity == null) {
  Log.e("WOOTRIC", "Current activity is null");
  return;
}

try {
  wootric = Wootric.init(currentActivity, clientId, accountToken);
} catch (Exception e) {
  Log.e("WOOTRIC", e.toString());
}

}

谁能帮我解决上面的问题

您可以在主线程上执行 Wootric 初始化代码,如下所示:

currentActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // initialize Wootric here
    }
});