androidreact-nativereact-native-native-module

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


I am using Wootrick SDK for create a bridge to react native. When I'm calling a specific function I'm getting following log and Wootric Survey doesn't show.

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

My code as follows,

@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());
}

}

Can someone help me to solve the above issue.


Solution

  • You can execute the Wootric initialization code on the main thread with something like this:

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