首页 > 解决方案 > Realm React Native 崩溃

问题描述

我是第一次使用Realm,我已经写了简单的代码app.js

import React, { Component } from 'react';

import { StyleSheet, Platform, View, Image, Text, TextInput, TouchableOpacity, Alert } from 'react-native';

var Realm = require('realm');

let realm ;

export default class App extends Component{

  constructor(){

    super();

    this.state = {

      Student_Name : '',

      Student_Class : '',

      Student_Subject : ''

  }

    realm = new Realm({
      schema: [{name: 'Student_Info',
      properties:
      {
        student_id: {type: 'int',   default: 0},
        student_name: 'string',
        student_class: 'string',
        student_subject: 'string'
      }}]
    });

  }

  add_Student=()=>{


    realm.write(() => {

      var ID = realm.objects('Student_Info').length + 1;

       realm.create('Student_Info', {
         student_id: ID,
         student_name: this.state.Student_Name,
         student_class: this.state.Student_Class,
         student_subject : this.state.Student_Subject
        });

    });

    Alert.alert("Student Details Added Successfully.")

  }

  render() {

    var A = realm.objects('Student_Info');

    var myJSON = JSON.stringify(A);

    return (

        <View style={styles.MainContainer}>

          <TextInput
                placeholder="Enter Student Name"
                style = { styles.TextInputStyle }
                underlineColorAndroid = "transparent"
                onChangeText = { ( text ) => { this.setState({ Student_Name: text })} }
              />

          <TextInput
                placeholder="Enter Student Class"
                style = { styles.TextInputStyle }
                underlineColorAndroid = "transparent"
                onChangeText = { ( text ) => { this.setState({ Student_Class: text })} }
              />

          <TextInput
                placeholder="Enter Student Subject"
                style = { styles.TextInputStyle }
                underlineColorAndroid = "transparent"
                onChangeText = { ( text ) => { this.setState({ Student_Subject: text })} }
              />


          <TouchableOpacity onPress={this.add_Student} activeOpacity={0.7} style={styles.button} >

            <Text style={styles.TextStyle}> CLICK HERE TO ADD STUDENT DETAILS </Text>

          </TouchableOpacity>


          <Text style={{marginTop: 10}}>{myJSON}</Text>


        </View>

    );
  }
}

const styles = StyleSheet.create({

 MainContainer :{

  flex:1,
  alignItems: 'center',
  justifyContent: 'center',
  paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
  margin: 10

  },

  TextInputStyle:
    {
      borderWidth: 1,
      borderColor: '#009688',
      width: '100%',
      height: 40,
      borderRadius: 10,
      marginBottom: 10,
      textAlign: 'center',
    },

  button: {

      width: '100%',
      height: 40,
      padding: 10,
      backgroundColor: '#4CAF50',
      borderRadius:7,
      marginTop: 12
    },

  TextStyle:{
      color:'#fff',
      textAlign:'center',
    }

});

现在我在图片http://prntscr.com/mym0zo中遇到这样的错误,正如我已经说过的,我是第一次使用领域,所以无法理解这个问题。

请帮我解决这个问题。

我的依赖是“依赖”:{“react”:“16.8.3”,“react-native”:“0.59.1”,“realm”:“^2.25.0”},

标签: javascriptandroidiosreact-nativerealm

解决方案


推荐阅读