React-native Android:调用AppRegistry.runApplication时出错

Der*_*erd 10 javascript android ecmascript-6 react-native

我真的不知道这里发生了什么.我已经设置了一个基本应用程序并使用了此处的代码共享方法.这一切都非常基础,所以这里是代码:

// index.android.js
// index.ios.js
import React, { AppRegistry } from 'react-native';
import CompetitionAgent from './app/index';

AppRegistry.registerComponent('CompetitionAgent', () => CompetitionAgent);
Run Code Online (Sandbox Code Playgroud)

和组件:

//./app/index.js
import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    TextInput,
    View
} from 'react-native';

export default class CompetitionAgent extends Component {
    constructor() {
        super();
        this.state = {nickname:''};
    }
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.information}>
                    <Text style={styles.welcome}>
                        Welcome to the Competition Agent Connect app!
                    </Text>
                    <Text style={styles.instructions}>
                        When you are near a Competition Agent, you can join the session.
                    </Text>
                </View>
                <View style={{padding:10}}>
                    <TextInput style={styles.inputStyle} />
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
    },
    information: {
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
    inputStyle: {
        flexDirection: 'row',
        backgroundColor: '#3E3134',
        color: '#FFFFFF',
    }
});
Run Code Online (Sandbox Code Playgroud)

我知道错误可能很多.所以这个基本布局会产生相同的错误.

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    TextInput,
    View
} from 'react-native';

export default class CompetitionAgent extends Component {
    constructor() {
        super();
        this.state = {nickname:''};
    }
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.information}>
                    Welcome to the Competition Agent Connect app!
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
    },
    information: {
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    }
});
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

E/unknown:React: Exception in native call
                                              java.lang.RuntimeException: Error calling AppRegistry.runApplication
                                                  at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                  at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                                                  at android.os.Looper.loop(Looper.java:158)
                                                  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:208)
                                                  at java.lang.Thread.run(Thread.java:818)
                                               Caused by: com.facebook.jni.CppException: Could not get BatchedBridge, make sure your bundle is packaged correctly
                                                  at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) 
                                                  at android.os.Handler.handleCallback(Handler.java:739) 
                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                  at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) 
                                                  at android.os.Looper.loop(Looper.java:158) 
                                                  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:208) 
                                                  at java.lang.Thread.run(Thread.java:818) 
Run Code Online (Sandbox Code Playgroud)

它昨天运行得很好,重启Android Studio也没有帮助.

YSK*_*YSK 19

如果您是从Android Studio运行应用程序,那么您必须react-native start从您的react项目文件夹中使用命令行启动react-native packager .

您还必须使用设置Android端口转发adb reverse tcp:8081 tcp:8081.

你做过那些吗?

  • 很高兴听到它有效.值得一提的是,如果你使用`react-native run-android`(而不是Android studio)从命令行启动你的应用程序,那么它应该启动打包器并为你做端口转发.但它有其他问题,有时会阻止它开始...... (2认同)

小智 5

当我为你设置正确的路径时,它帮助了我

ANDROID_HOME = C:\Users\username\AppData\Local\Android\sdk
Run Code Online (Sandbox Code Playgroud)

和工具:

%ANDROID_HOME%\build-tools
%ANDROID_HOME%\platform-tools
%ANDROID_HOME%\tools
Run Code Online (Sandbox Code Playgroud)