android - 在 react native 中导入 Realm 时未知的执行上下文

标签 android react-native realm

我对react-native很陌生,我正在尝试使用Realm。我已经完成了 react-native 链接 Realm rnpm 链接 Realm 。但是当我尝试导入 Realm 时,出现错误未知执行上下文,这是我的 index.android.js

import React, { Component } from 'react';
import {
 AppRegistry,
 StyleSheet,
 Text,
 View,
} from 'react-native';
import { TabViewAnimated, TabBar } from 'react-native-tab-view';
import Today from './app/Today'
import Realm from 'realm'
import _ from 'lodash'

const styles = StyleSheet.create({
container: {
    flex: 1,
},
page: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
},
});
export default class ExpenseManagerProject extends Component {
state = {
index: 0,
routes: [
{ key: '1', title: 'Today' },
{ key: '2', title: 'Category' },
{ key: '3', title: 'Date' },
],
};
_handleChangeTab = (index) => {
this.setState({ index });
 };

_renderFooter = (props) => {
  return <TabBar {...props} />;
};

_renderScene = ({ route }) => {
switch (route.key) {
    case '1':
    return <Today/>;
    case '2':
    return <View style={[ styles.page, { backgroundColor: '#673ab7' } ]} />;
    default:
    return null;
}
};

render() {
return (
     <TabViewAnimated
      style={styles.container}
      navigationState={this.state}
      renderScene={this._renderScene}
      renderFooter={this._renderFooter}
      onRequestChangeTab={this._handleChangeTab}
      />
  );
}
}

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

最佳答案

根据documentation:

Add the following lines to android/settings.gradle:

gradle include ':realm' project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')

Add the compile line to the dependencies in android/app/build.gradle:

gradle dependencies { compile project(':realm') }

Add the import and link the package in android/app/src/main/java/com/[your-application-name]/MainApplication.java:

import io.realm.react.RealmReactPackage; // add this import
public class MainApplication extends Application implements ReactApplication {
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new RealmReactPackage() // add this line
        );
    }
}

对我来说,这只是最后一步,将行添加到 MainApplication.java 中,这是我必须做的,因为其他内容已经存在。因此,请务必检查提到的所有文件。

如果还是不行,新建一个项目,添加Realm(包括上面的步骤),添加之前写的所有文件。

关于android - 在 react native 中导入 Realm 时未知的执行上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557984/

相关文章:

javascript - 主目录之外的 React-Native 导入文件

react-native - AWS 放大 : How to resend code when Auth 'signIn' API returns 'UserNotConfirmedException'

swift - 如何在Realm数据库中保存多级关系

android - 从主 Activity 向 SlidingMenu 上的 ListView 添加项目

android - LayerDrawable OutOfMemoryError

javascript - ECMA 6、React Native 和 Redux : what does this syntactic sugar mean?

node.js - 通过npm安装Realm时出错: npm ERR!在realm@1.2.0安装脚本失败 'node-pre-gyp install --fallback-to-build'

realm - 如何使用预填充的 Realm 数据库交付应用程序

Android SDK 管理器不显示要安装的包

android - HandlerThread vs Executor - 什么时候比另一个更合适?