我们正在尝试将react-native集成到我们现有的Android应用程序中: build.gradle的SDK要求是: minSdkVersion 16 targetSdkVersion 23
我们使用Intent来调用最初的React index.js:
Intent inte = new Intent(this, ReactNativeStarter.class);
startActivity(inte);
其余代码来自react integration tutorial。
当我们尝试调用反应组件时,我们会收到以下错误:
Caused by: java.lang.IllegalAccessError: Method 'void android.support.v4.net.ConnectivityManagerCompat.<init>()' is inaccessible to class 'com.facebook.react.modules.netinfo.NetInfoModule'
(declaration of 'com.facebook.react.modules.netinfo.NetInfoModule' appears in /data/data/at.??????/files/instant-run/dex/slice-com.facebook.react-react-native-0.20.1_3762d580ab3ced1fa2f7503493d38e666994b9fa-classes.dex)
at com.facebook.react.modules.netinfo.NetInfoModule.<init>(NetInfoModule.java:55)
你能帮忙吗?
完整错误日志:
12-19 10:07:22.157 30271-32680/at.???? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: at.?????, PID:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalAccessError: Method 'void android.support.v4.net.ConnectivityManagerCompat.<init>()' is inaccessible to class 'com.facebook.react.modules.netinfo.NetInfoModule'
(declaration of 'com.facebook.react.modules.netinfo.NetInfoModule' appears in /data/data/at.??????/files/instant-run/dex/slice-com.facebook.react-react-native-0.20.1_3762d580ab3ced1fa2f7503493d38e666994b9fa-classes.dex)
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:793)
at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:730)
at com.facebook.react.ReactInstanceManagerImpl.access$600(ReactInstanceManagerImpl.java:91)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:184)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:169)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
我怀疑这是由于gradle拾取错误版本的react-native所致,因为maven allprojects配置中的路径错误(至少对我们而言) 在这里看PR:https://github.com/facebook/react-native/pull/14743
答案 1 :(得分:0)
我正在将现有的Android应用程序(用kotlin编写)与React Native集成。
我为运行ReactRootView
而编写的活动(.kt)具有以下onCreate()
方法:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mReactRootView = ReactRootView(this)
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(application)
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index")
.addPackage(MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState((LifecycleState.RESUMED))
.build()
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView!!.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null)
setContentView(mReactRootView)
if (Build.VERSION.SDK_INT >= M) {
if (!canDrawOverlays(this)) {
val intent = Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName"))
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE)
}
}
}
问题恰到好处
mReactRootView!!.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null)
。
我必须将“模块”名称从“ MyReactNativeApp”更改为“ App”,这是我在React上注册的主要组件的名称。
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
很好...右边的onCreate()
功能是
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mReactRootView = ReactRootView(this)
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(application)
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index")
.addPackage(MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState((LifecycleState.RESUMED))
.build()
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView!!.startReactApplication(mReactInstanceManager, "App", null)
setContentView(mReactRootView)
if (Build.VERSION.SDK_INT >= M) {
if (!canDrawOverlays(this)) {
val intent = Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName"))
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE)
}
}
}