android - React Native Android 桥错误 : Method addObserver must be called on the main thread

标签 android react-native react-native-native-module

我正在使用 Wootrick SDK 创建一个桥梁来响应 native 。当我调用特定函数时,我会收到以下日志,但 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
    }
});

关于android - React Native Android 桥错误 : Method addObserver must be called on the main thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70589874/

相关文章:

react-native - React - 从另一个组件继承/复制 propTypes

ios - React Native项目中的 super 表达错误

javascript - FlatList contentContainerStyle -> justifyContent : 'center' causes issues with scrolling

reactjs - 我们是否需要使用 React Native 0.60 运行 `pod install`?

java - 如何将服务器设置为 "deploy"以便可以通过 Internet 访问它?

java - 如何通过Java在Android中使用可绘制形状?

android - 对于新闻应用程序的文章页面,我更喜欢将照片沿着文章文本放在右侧,而不是居中

android - 如何在滑动页面时播放短音?

react-native - React Native 使用 Native Modules 获取电池状态/电量

android - AudioManager setCommunicationDevice 不适用于 Android 12