[英]Cannot find symbol getCurrentActivity when building an Android module for React-Native
First of all, here is a test repo to replicate my issue 首先,这是一个测试仓库来复制我的问题
Hello there, I'm trying to create a simple Android module for React-Native and I'm having some trouble getting the current Activity from the Java code. 您好,我正在尝试为React-Native创建一个简单的Android模块,并且在从Java代码获取当前Activity时遇到了一些麻烦。
This is not the first native module I worked on, but I never had to obtain an Activity reference since today. 这不是我研究的第一个本机模块,但是从今天开始,我再也不需要获得Activity参考。
The interested module is a bridge to the Estimote SDK, but this is not relevant to the issue. 感兴趣的模块是Estimote SDK的桥梁,但这与问题无关。
The module in Android studio works perfectly, but when I try to build it with react-native run android
I get the following error: Android Studio中的模块工作正常,但是当我尝试使用
react-native run android
构建它时,出现以下错误:
:react-native-estimote-android:compileReleaseJavaWithJavac
/Users/matteo/dev/react-native-example/react-native-estimote-android/android/src/main/java/com/mmazzarolo/estimoteandroid/EstimoteAndroidModule.java:64: error: cannot find symbol
Activity currentActivity = this.getCurrentActivity();
^
symbol: method getCurrentActivity()
1 error
:react-native-estimote-android:compileReleaseJavaWithJavac FAILED
FAILURE: Build failed with an exception.
From my understanding extending ReactContextBaseJavaModule should be enough to grant me the usage of this.getCurrentActivity() (and Android studio agrees with me). 根据我的理解,扩展ReactContextBaseJavaModule应该足以授予我this.getCurrentActivity ()的用法(Android Studio同意我的观点)。
This is what I already tried to do: 这是我已经尝试做的事情:
- Implementing ActivityEventListener : same cannot find symbol error when I try to import ActivityEventListener; -实现ActivityEventListener :当我尝试导入ActivityEventListener时,同样找不到符号错误;
- android/.gradlew clean -android / .gradlew干净
- watchman watch-del-all && rm -rf node_modules/ && npm cache clean && npm prune && npm i -watchman watch-del-all && rm -rf node_modules / && npm缓存清理&& npm修剪&& npm i
- Tried the same repo on two different computers... -在两台不同的计算机上尝试了相同的仓库...
Any hints? 有什么提示吗? Thank you in advance.
先感谢您。
the issue was with the version of react-native in module 'react-native-estimote-android' 问题是模块'react-native-estimote-android'中的react-native版本
Just change compile 'com.facebook.react:react-native:0.12.+'
to compile 'com.facebook.react:react-native:+'
只需将
compile 'com.facebook.react:react-native:0.12.+'
更改为compile 'com.facebook.react:react-native:+'
This will resolve the cannot find getCurrentActivity symbol. 这将解决找不到getCurrentActivity符号的问题。
getCurrentActivity method is not available in react-native 0.12 version. 在react-native 0.12版本中, getCurrentActivity方法不可用。 Thats why it was throwing error.
这就是为什么它引发错误。
@ReactMethod
public void start(final Callback callback) {
Activity currentActivity = getCurrentActivity();
this.mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
mBeaconManager.startRanging(region);
callback.invoke();
}
});
}
Remove this from start method, if you do this.getCurrentActivity() it will referr to EstimoteAndroidModule context not to the app ReactContextBaseJavaModule context 从start方法中删除它,如果执行this.getCurrentActivity(),它将引用EstimoteAndroidModule上下文而不是应用程序ReactContextBaseJavaModule上下文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.