简体   繁体   English

为React-Native构建Android模块时找不到符号getCurrentActivity

[英]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.

相关问题 setRequestedOrientation无法在react-native中找到符号 - setRequestedOrientation cannot find symbol in react-native 在 MainActivity.java React-native Android 中找不到符号 - Can not find symbol in MainActivity.java React-native Android 使用新类为 Android 构建 Chromium 时出现“错误:找不到符号” - "error: cannot find symbol" when building Chromium for Android with new classes 在 flutter 中构建 Android 时出现“错误:找不到符号” - "error: cannot find symbol" when building for Android in flutter Java 找不到“/platform-tools/adb”(使用 react-native 构建应用程序) - Java cannot find '/platform-tools/adb' (Building app with react-native) react-native run-android 构建错误 ':app:generateDebugBuildConfig' - react-native run-android Building Error ':app:generateDebugBuildConfig' 找不到符号MODULE $ - cannot find symbol MODULE$ 构建Mopub SDK时,找不到符号:类org.fest.assertions.api.ANDROID - When building mopub sdk, cannot find symbol: class org.fest.assertions.api.ANDROID 从Java模块启动react-native活动(android) - Starting react-native activity from java module (android) [错误]:使用 maven 和 lombok 构建时找不到符号变量日志 - [ERROR]: cannot find symbol variable log when building with maven and lombok
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM