React Native与现有Android应用程序集成闪回

时间:2016-05-31 02:51:30

标签: android react-native integrate

我会尽力解释我的问题并提供足够的信息来展示细节。 首先,粘贴错误日志(FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()):

FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                          at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
                                                                                          at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:239)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                                          at java.lang.Thread.run(Thread.java:841)
                                                                                       Caused by: java.lang.IllegalAccessError: tried to access method android.support.v4.net.ConnectivityManagerCompat.<init>:(Lcom/facebook/react/bridge/ReactApplicationContext;)V from class com.facebook.react.modules.netinfo.NetInfoModule
                                                                                          at com.facebook.react.modules.netinfo.NetInfoModule.<init>(NetInfoModule.java:55)
                                                                                          at com.facebook.react.shell.MainReactPackage.createNativeModules(MainReactPackage.java:67)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl.processPackage(ReactInstanceManagerImpl.java:751)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:688)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl.access$600(ReactInstanceManagerImpl.java:84)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:177)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:162)
                                                                                          at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                                          at java.lang.Thread.run(Thread.java:841)

我的项目结构:

project structrue

android part

android依赖项:

dependencies {
    ...
    compile 'com.facebook.react:react-native:0.20.0'
}

的AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>

MainActivity.java:

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {

    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mReactRootView = new ReactRootView(this);
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(new MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        mReactRootView.startReactApplication(mReactInstanceManager, "AndroidToRn03", null);

        setContentView(mReactRootView);
    }
    ...
}

的package.json:

{
  "name": "androidtorn03",
  "version": "1.0.0",
  "description": "",
  "main": "index.android.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react-native": "^0.26.2"
  }
}

然后,index.android.js:

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

class AndroidToRn03 extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.hello}>React Native....</Text>
      </View>
    )
  }
}
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
  hello: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

AppRegistry.registerComponent('AndroidToRn03', () => AndroidToRn03);

我根据docs on facebook's website尝试了很多次,但都失败了,应用程序在打开时闪回。 我需要你的帮助!你介意花些时间来帮助我吗?

1 个答案:

答案 0 :(得分:0)

更改文件app/build.gradle依赖项:

compile 'com.android.support:appcompat-v7:23.0.1'