gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:46 26 4
gpt4 key购买 nike

我真的是 react-native 的新手,我正在尝试使用 Realm。我已经完成了 react-native link realmrnpm link realm。但是当我尝试导入 Realm 时出现错误 unknown execution context,这是我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com