无法在安卓设备上运行React-Native示例代码

3

我刚接触JavaScript和React-Native尝试创建演示应用程序。该应用程序在模拟器中正常运行。以下是相同的截屏: 模拟器截图

但是,如果我尝试在我的设备上运行应用程序,则应用程序无法运行。以下是相同的截屏:

设备截图

请告诉我是否需要提交任何代码。我尝试寻找上述问题的解决方案,但未能找到任何信息。

以下是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);

如果我有任何错误,请告诉我。


也许这个链接可以帮到你:https://dev59.com/T1wY5IYBdhLWcg3weXzV - Ali Raza
@AliRaza 非常感谢。请将此作为答案发布,以便我可以接受它。 - Rajesh Bhagat
3个回答

1

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

在你的服务器运行时(react-native start),将JS文件捆绑到你的apk中,并将捆绑文件下载到你的应用程序的assets目录中:

以下是解决方案链接:

react native android failed to load JS bundle


0

0

我也遇到了同样的问题,我通过启动本地服务器http-server -c-1解决了这个问题,然后运行以下命令'adb反向tcp:8081 tcp:8081',最后再运行react-native run-android


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接