英文:
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 'BeaconModule' 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<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:
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 'Context' and 'ReactApplicationContext'
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论