本机模块是空对象



我制作了一个简单的本机模块(Android),用于显示吐司。我已经将模块包添加到了ReactInstanceManager中。但是,当我记录NativeModules对象时,它是空的。我尝试清洁和重建该项目,但NativeModules对象总是空的。我正在附加下面的代码。

simpletoastmodule.java

package com.example.shrijit.weteach11;
import android.widget.Toast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class SimpleToastModule extends ReactContextBaseJavaModule {
    public SimpleToastModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }
    @Override
    public String getName() {
        return "SimpleToastModule";
    }
    @ReactMethod
    public void show(String message) {
        Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }
}

simpletoastpackage.java

package com.example.shrijit.weteach11;
import com.facebook.react.ReactPackage;
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;
public class SimpleToastPackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new SimpleToastModule(reactContext));
        return modules;
    }
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}

mainActivity.java

package com.example.shrijit.weteach11;
import android.app.Activity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.KeyEvent;
import com.facebook.react.ReactApplication; 
import com.facebook.react.ReactInstanceManager; 
import com.facebook.react.ReactNativeHost; 
import com.facebook.react.ReactPackage; 
import com.facebook.react.ReactRootView; 
import com.facebook.react.common.LifecycleState; 
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; 
import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays; import java.util.List;
public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    @Override
    protected void onPause() {
        super.onPause();
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostPause(this);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostDestroy(this);
        }
        if (mReactRootView != null) {
            mReactRootView.unmountReactApplication();
        }
    }
    @Override
    public void onBackPressed() {
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onBackPressed();
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager.showDevOptionsDialog();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReactRootView = new ReactRootView(this);
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModulePath("index")
                .addPackages(Arrays.asList(new MainReactPackage(), new SimpleToastPackage()))
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        // The string here (e.g. "WeTeach11") has to match
        // the string in AppRegistry.registerComponent() in index.js
        Bundle initialProps = new Bundle();
        initialProps.putString("msg", "this is from android land");
        mReactRootView.startReactApplication(mReactInstanceManager, "WeTeach11", initialProps);
        setContentView(mReactRootView);
    }
    @Override
    public void invokeDefaultOnBackPressed() {
        super.onBackPressed();
    } 
}

app.js

import * as React from 'react';
import { View, DrawerLayoutAndroid, Prop } from 'react-native';
import { NativeRouter } from 'react-router-native';
import { Portal } from 'react-native-paper';
import md5 from 'react-native-md5';
import PropTypes from 'prop-types';
import DrawerMenu from './components/DrawerMenu';
import ToplevelView from './components/TopLevelView';
import {NativeModules} from 'react-native';
export class App extends React.Component {
  static propTypes = {
    msg: PropTypes.string,
  };
  constructor(props) {
    super(props);
    this.closeDrawer = () => {
      this.drawer.closeDrawer();
    };
    this.openDrawer = () => {
      this.drawer.openDrawer();
    };
  }
  componentDidMount() {
    //console.debug('props.msg:', this.props.msg);
    console.debug(NativeModules); // this is empty ie: {}
  }
  render() {
    var navigationView = (
      <View style={{ flex: 1, backgroundColor: '#dfdfdf' }}>
        <DrawerMenu closeDrawer={this.closeDrawer} />
      </View>
    );
    return (
      <Portal.Host>
        <NativeRouter>
          <DrawerLayoutAndroid
            ref={e => (this.drawer = e)}
            drawerWidth={300}
            drawerPosition={DrawerLayoutAndroid.positions.Right}
            renderNavigationView={() => navigationView}>
            <ToplevelView openDrawer={this.openDrawer} />
          </DrawerLayoutAndroid>
        </NativeRouter>
      </Portal.Host>
    );
  }
}
export default App;

有人可以通过指出我在做什么错,可以帮助我吗?任何帮助都将受到赞赏。

确保您尚未使用Expo创建该应用程序,您不能将本机模块与Expo应用程序使用。在这里回答,因为我无法评论。

相关内容

  • 没有找到相关文章

最新更新