React Native中的本地模块,在Java类中无法调用”this”。

huangapple 未分类评论46阅读模式
英文:

Native module in react native, cannot call "this" in java class

问题

帮帮我...!
在我的 React Native 项目中,我创建了一个名为 'BeaconModule' 的原生模块,用于使用一些 React Native 不支持的功能。在 Android 项目中,它正常工作。但是当我将函数 AIBeaconManager.init() 复制到 React Native 项目中时,出现了以下错误。

* 这是我的代码
```java
public class BeaconModule extends ReactContextBaseJavaModule implements AIBeaconListener{
  private static ReactApplicationContext reactContext;

  BeaconModule(ReactApplicationContext context) {
    super(context);
    reactContext = context;

    Map<String, Object> options = new HashMap<String, Object>() {{
			put(AIBeaconManagerInitOption.Keys.AutoServiceRegistrationEnabled, Boolean.valueOf(true));
			put(AIBeaconManagerInitOption.Keys.PushNotificationEnabled, Boolean.valueOf(true));
		}};
    AIBeaconManager aiBeaconManager = AIBeaconManager.init(this, "1062", "09CYvck2rZVNlpQI", options);
  }
  • 错误:
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
/Users/admin/Documents/Working/GPass/GPass/android/app/src/main/java/com/gpass/BeaconModule.java:57: error: incompatible types: BeaconModule cannot be converted to Context
    AIBeaconManager aiBeaconManager = AIBeaconManager.init(this, "1062", "09CYvck2rZVNlpQI", options); 
                                                           ^
Note: /Users/admin/Documents/Working/GPass/GPass/android/app/src/debug/java/com/gpass/ReactNativeFlipper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task 'app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

我认为这是因为 'Context' 和 'ReactApplicationContext' 之间的差异。


<details>
<summary>英文:</summary>

help meeee....!
In my react native project, and created a native module &#39;BeaconModule&#39; to use some function that react native does not support. in android project, it works fine. but when i coppy function AIBeaconManager.init() to reactnative project, it got some error bellow.

* this is my code

public class BeaconModule extends ReactContextBaseJavaModule implements AIBeaconListener{
private static ReactApplicationContext reactContext;

BeaconModule(ReactApplicationContext context) {
super(context);
reactContext = context;

Map&lt;String, Object&gt; options = new HashMap&lt;String, Object&gt;() {{
		put(AIBeaconManagerInitOption.Keys.AutoServiceRegistrationEnabled, Boolean.valueOf(true));
		put(AIBeaconManagerInitOption.Keys.PushNotificationEnabled, Boolean.valueOf(true));
	}};
AIBeaconManager aiBeaconManager = AIBeaconManager.init(this, &quot;1062&quot;, &quot;09CYvck2rZVNlpQI&quot;, options);

}


* error:

Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
/Users/admin/Documents/Working/GPass/GPass/android/app/src/main/java/com/gpass/BeaconModule.java:57: error: incompatible types: BeaconModule cannot be converted to Context
AIBeaconManager aiBeaconManager = AIBeaconManager.init(this, "1062", "09CYvck2rZVNlpQI", options);
^
Note: /Users/admin/Documents/Working/GPass/GPass/android/app/src/debug/java/com/gpass/ReactNativeFlipper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org


I think it because difference between &#39;Context&#39; and &#39;ReactApplicationContext&#39;

</details>


huangapple
  • 本文由 发表于 2020年4月10日 16:57:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61137007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定