Rajesh Bhagat
Rajesh Bhagat

Reputation: 43

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

Im new to javaScript and React-Native tried creating demo application. The application runs properly in Emulator. Following is the screenshot of the same: Emulator Screenshot

while if i try to run application on my device the application doesn't run. Following is the screenshot of the same:

Device Screenshot

Please let me know if any code need to be posted. i tried searching for solution of above mention issue,but not able to find any

Following is the JavaScript code:

/**
* 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);

Please let me know if i'm making mistake anywhere.

Upvotes: 3

Views: 1389

Answers (3)

Deepak Patidar
Deepak Patidar

Reputation: 394

I also face the same issue I resolved it with start localhost server http-server -c-1 than run this command 'adb reverse tcp:8081 tcp:8081' after that run react-native run-android

Upvotes: 0

Ali Raza
Ali Raza

Reputation: 147

Ok i am just copying it from that post :)

To bundle JS file into your apk while having your server running (react-native start) download bundle into assets directory of your app:

Following is the link to the solution:

react native android failed to load JS bundle

Upvotes: 1

rob-art
rob-art

Reputation: 667

For running an application on device you need to use the development server or an offline bundle.

Everything is explain here for ios : https://facebook.github.io/react-native/docs/running-on-device-ios.html

Here for Android : https://facebook.github.io/react-native/docs/running-on-device-android.html#content

Upvotes: 0

Related Questions