0

I have a problem when trying to bridging my React Native Project to Java. Here is my code:

my ReactContextBaseJavaModule :

public class analyticsEvent extends ReactContextBaseJavaModule{
private static ReactApplicationContext reactContext;

mFirebaseAnalyticsClass classEvent = new mFirebaseAnalyticsClass();


public analyticsEvent(ReactApplicationContext context){
    super(context);
    reactContext = context;
}

@ReactMethod
public void productClick(String itemID, String itemName , String itemCategory , String itemVariant,
                         String itemBrand , String itemPrice ,String itemCurrency , String itemIndex
                         ){

    Bundle product1 = new Bundle();
    product1.putString(Param.ITEM_ID, itemID);
    product1.putString(Param.ITEM_NAME, itemName);
    product1.putString(Param.ITEM_CATEGORY, itemCategory);
    product1.putString(Param.ITEM_VARIANT, itemVariant);
    product1.putString(Param.ITEM_BRAND, itemBrand);
    product1.putString(Param.PRICE, itemPrice);
    product1.putString(Param.CURRENCY, itemCurrency);
    product1.putString(Param.INDEX, itemIndex);

    Bundle ecommerceBundle = new Bundle();
    ecommerceBundle.putBundle("items", product1);

    classEvent.analyticsFirebase(ecommerceBundle);


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

}

my Package:

public  class analyticsEventPackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(
        ReactApplicationContext reactContext) {

    List<NativeModule> modules = new ArrayList<>();

    modules.add(new analyticsEvent(reactContext));  //THIS CODE CAUSE THE PROBLEM

    return modules;
}

@NonNull
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}

MainApplication:

public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost =
    new ReactNativeHost(this) {
      @Override
      public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
     }

    @Override
      protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
       packages.add(new analyticsEventPackage());
       return packages;
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
    };

  @Override
   public ReactNativeHost getReactNativeHost() {
   return mReactNativeHost;
  }

  @Override
  public void onCreate() {
  super.onCreate();
   SoLoader.init(this, /* native exopackage */ false);
   initializeFlipper(this); // Remove this line if you don't want Flipper enabled
  }

  /**
  * Loads Flipper in React Native templates.
  *
  *  @param context
  */
  private static void initializeFlipper(Context context) {
  if (BuildConfig.DEBUG) {
   try {
     /*
     We use reflection here to pick up the class that initializes Flipper,
    since Flipper library is not available in release mode
    */
    Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
    aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
     } catch (ClassNotFoundException e) {
     e.printStackTrace();
     } catch (NoSuchMethodException e) {
     e.printStackTrace();
     } catch (IllegalAccessException e) {
     e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
  }
 }

}

When i tried to add module, it always give me the "Looper.prepare()" error. Many people said that i have to use Handler, but i don't know where i should put the Handler. Please help me guys, thank you.

  • Does this answer your question? [Can't create handler inside thread that has not called Looper.prepare()](https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – Md. Asaduzzaman Jan 27 '20 at 11:58
  • nope, because i don't have any code that related to UI. I am just trying to sending event to Google Analytics, that's all. @Md.Asaduzzaman – Novando Santosa Jan 27 '20 at 12:33

0 Answers0