繁体   English   中英

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

[英]Not able to run React-Native sample code on Android device

我是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);

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

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

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

以下是解决方案的链接:

反应原生android无法加载JS包

要在设备上运行应用程序,您需要使用开发服务器或脱机捆绑包。

一切都在这里解释为ios: https//facebook.github.io/react-native/docs/running-on-device-ios.html

适用于Android: https//facebook.github.io/react-native/docs/running-on-device-android.html#content

我也遇到了同样的问题我用启动localhost服务器http-server -c-1解决了它比运行这个命令'adb reverse tcp:8081 tcp:8081'之后运行react-native run-android

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM