原生调用RN的时候 module报空指针



  • btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    MyReactNativePackage nativePackage = MyApplication.getNativePackage();
    MyReactNativeModule nativeModule = nativePackage.mModule;
    nativeModule.callRNFun();
    }
    });

    public class MyReactNativePackage implements ReactPackage {
    public MyReactNativeModule mModule;

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        Log.i("TAG", "ReactPackage.createNativeModules");
        List<NativeModule> list = new ArrayList<>();
        mModule = new MyReactNativeModule(reactContext);
        list.add(mModule);
        return list;
    }
    
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
    

    package com.android.sjq.arndemo_0820_01;

    import android.app.Application;
    import android.util.Log;

    import com.facebook.react.ReactApplication;
    import com.facebook.react.ReactNativeHost;
    import com.facebook.react.ReactPackage;
    import com.facebook.react.shell.MainReactPackage;
    import com.facebook.soloader.SoLoader;

    import java.util.Arrays;
    import java.util.List;

    public class MyApplication extends Application implements ReactApplication {

    static MyReactNativePackage mNativePackage = new MyReactNativePackage();
    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }
    
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    mNativePackage
            );
        }
    
        @Override
        protected String getJSMainModuleName() {
            return "index";
        }
    };
    
    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }
    
    public static MyReactNativePackage getNativePackage() {
        Log.i("TAG", "MyApplication.getNativePackage");
        return mNativePackage;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        SoLoader.init(this, /* native exopackage */ false);
    }
    

    }


Log in to reply