gpt4 book ai didi

javascript - 无法在 Android 设备上运行 React-Native 示例代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:42 26 4
gpt4 key购买 nike

我是 javaScript 的新手,React-Native 尝试创建演示应用程序。该应用程序在模拟器中正常运行。以下是相同的屏幕截图: Emulator Screenshot

而如果我尝试在我的设备上运行应用程序,应用程序不会运行。以下是相同的屏幕截图:

Device Screenshot

如果需要发布任何代码,请告诉我。我尝试寻找上述问题的解决方案,但找不到任何

JavaScript代码如下:

/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';

var React = require('react-native');
var {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} = React;

var API_KEY = '7waqfqbprs7pajbz28mqf6vz';
var API_URL = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json';
var PAGE_SIZE = 25;
var PARAMS = '?apikey=' + API_KEY + '&page_limit=' + PAGE_SIZE;
var REQUEST_URL = API_URL + PARAMS;




var Button = require('react-native-icon-button');
var AwesomeProject = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
};
},

componentDidMount: function() {
this.fetchData();
},

fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData.movies),
loaded: true,
});
})
.done();
},

render: function() {
if (!this.state.loaded) {
return this.renderLoadingView();
}

return (
<View style={styles.mainLayout}>
<Button
style={styles.button}
onPress={this._handlePress}>
color={"white"}
iconSize={20}
text={"Press me!"}
</Button>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderMovie}
style={styles.listView}/>
</View>

);
},
_handlePress(event){
console.log('Pressed');
},
renderLoadingView: function() {
return (
<View style={styles.container}>
<Text>
Loading movies...
</Text>
</View>
);
},

renderMovie: function(movie) {
return (
<View style={styles.container}>
<Image
source={{uri: movie.posters.thumbnail}}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
);
},
});

var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
rightContainer: {
flex: 1,
},
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
thumbnail: {
width: 53,
height: 81,
},
listView: {
paddingTop: 20,
backgroundColor: '#F5FCFF',
},
mainLayout:{
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
padding: 10,
borderRadius: 5,
backgroundColor: '#272822',
color: 'white'
},
});

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

如果我在任何地方犯了错误,请告诉我。

最佳答案

好的,我只是从那篇文章中复制它:)

要在服务器运行时将 JS 文件 bundle 到 apk 中(react-native 启动),请将 bundle 下载到应用程序的 Assets 目录中:

以下是解决方案的链接:

react native android failed to load JS bundle

关于javascript - 无法在 Android 设备上运行 React-Native 示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102656/

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