Android原生应用整合react native,然后用react native 调用原生发生异常



  • 在原生应用中嵌入React Native,然后,用react native来调用原生时发生了异常。react native调用原生是按官方给的教程:
    http://facebook.github.io/react-native/docs/native-modules-android.html
    其中将ToastModule名称修改为NativeActivity,getName()也进行了相应更改。

    报的错误时:undefined is no an object(evaluating '_NativeActivity2.default.newNativeActivity')。

    具体代码
    NativeActivity:
    package com.example.third.nativeactivity;

    import android.content.Intent;

    import com.example.third.MyApp;
    import com.facebook.react.bridge.ReactApplicationContext;
    import com.facebook.react.bridge.ReactContextBaseJavaModule;
    import com.facebook.react.bridge.ReactMethod;

    /**

    • Created by Mini-zy on 16/7/18.
      */
      public class NativeActivity extends ReactContextBaseJavaModule {
      public NativeActivity(ReactApplicationContext reactContext) {
      super(reactContext);
      }

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

      @ReactMethod
      public void newNativeActivity(){
      Intent intent = new Intent(MyApp.getContext(), Main2Activity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      MyApp.getContext().startActivity(intent);
      }
      }

    FirstReactPackage:
    package com.example.third.nativeactivity;

    import com.facebook.react.ReactPackage;
    import com.facebook.react.bridge.JavaScriptModule;
    import com.facebook.react.bridge.NativeModule;
    import com.facebook.react.bridge.ReactApplicationContext;
    import com.facebook.react.uimanager.ViewManager;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    /**

    • Created by Mini-zy on 16/7/18.
      */
      public class FirstReactPackage implements ReactPackage {
      @Override
      public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
      List<NativeModule> modules = new ArrayList<>();
      modules.add(new NativeActivity(reactContext));
      return modules;
      }

      @Override
      public List<Class<? extends JavaScriptModule>> createJSModules() {
      return Collections.emptyList();
      }

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

    NativeActivity.js:
    'use strict';

    import { NativeModules } from 'react-native';
    module.exports = NativeModules.NativeActivity;

    index.android.js:
    'use strict';

    import React from 'react';
    import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity
    } from 'react-native';
    import NativeActivity from './NativeActivity';

    class HelloWorld extends React.Component {

    constructor(props){
    super(props);
    console.log(NativeActivity);
    }
    _newNativeActivity(){
    NativeActivity.newNativeActivity();
    }

    render() {
    return (
    <View style={styles.container}>
    <Text style={styles.hello}>Hello, World</Text>

        <TouchableOpacity onPress={this._newNativeActivity}>
          <Text>New Native Activity</Text>
        </TouchableOpacity>
      </View>
    )
    

    }
    }
    var styles = StyleSheet.create({
    container: {
    flex: 1,
    justifyContent: 'center',
    },
    hello: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    },
    });

    AppRegistry.registerComponent('HelloWorld', () => HelloWorld);



  • 要在调用js 代码的Activity中加载ReactPackage,例如:
    mReactRootView = new ReactRootView(this);
    mReactInstanceManager = ReactInstanceManager.builder()
    .setApplication(getApplication())
    .setBundleAssetName("index.android.bundle")
    .setJSMainModuleName("index.android")
    .addPackage(new MainReactPackage())
    .addPackage(new AppReactPackage())
    .setUseDeveloperSupport(BuildConfig.DEBUG)
    .setInitialLifecycleState(LifecycleState.RESUMED)
    .build();
    mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);

    注意到中间的.addPackage(...)了吗?



  • 请问有运行成功的源码吗?我rn和原生混编总是失败。。。