Why can't I call a Java method of an imported android platform api class in cordova cusotm plugin

huangapple go评论114阅读模式
英文:

Why can't I call a Java method of an imported android platform api class in cordova cusotm plugin

问题

插件的 Java 文件:

  1. package cordova.plugin.hello;
  2. import org.apache.cordova.CordovaPlugin;
  3. import org.apache.cordova.CallbackContext;
  4. import org.json.JSONArray;
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7. // 我的导入
  8. import android.content.res.AssetManager;
  9. import android.content.Context;
  10. import java.io.InputStream;
  11. public class hello extends CordovaPlugin {
  12. @Override
  13. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  14. Context context = createPackageContext("io.ionic.starter", 0);
  15. AssetManager assetManager = context.getAssets();
  16. InputStream is = assetManager.open("pack1");
  17. }
  18. }

在构建应用程序时,我收到以下错误:

  1. > Task :app:compileDebugJavaWithJavac FAILED
  2. C:\Users\LUM\appAsset\platforms\android\app\src\main\java\cordova\plugin\hello\hello\hello.java:22: error: cannot find symbol
  3. Context context = createPackageContext("io.ionic.starter", 0);
  4. ^
  5. symbol: method createPackageContext(String,int)
  6. location: class hello
  7. Note: Some input files use or override a deprecated API.
  8. Note: Recompile with -Xlint:deprecation for details.
  9. 1 error

我对应用程序开发非常陌生,我在互联网上找到的所有可能的解决方案都会创建不同的错误,而且离你在第一个链接文章中看到的三行代码越远,问题就越多。例如,添加 this.cordova.getActivity() 并在其上调用 createPackageContext("io.ionic.starter", 0);。这甚至创建了更多的错误。其他修复尝试也是如此。

我明白即使修复其他错误后仍然会出现错误是正常的,但如果您能帮助我找出我在代码中基本上做错了什么,那将对我非常有帮助。

Android SDK 工具:26.1.1
NodeJS:v12.18.3
npm:6.14.6
操作系统:Windows 10
Gradle:6.6.1
Ionic 框架:@ionic/angular 5.3.3
Cordova 平台:android 9.0.0

  1. <details>
  2. <summary>英文:</summary>
  3. I am creating an App with Ionic and in order to load assets that are working with Play Asset Delivery I need to access the android native code by creating a cordova Plugin. I need to add those [three lines of code][1]. In order to achieve this I imported the Android Platform APIs that contain those methods and classes.
  4. Plugins Java file:
  5. package cordova.plugin.hello;
  6. import org.apache.cordova.CordovaPlugin;
  7. import org.apache.cordova.CallbackContext;
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11. //My Imports
  12. import android.content.res.AssetManager;
  13. import android.content.Context;
  14. import java.io.InputStream;
  15. public class hello extends CordovaPlugin {
  16. @Override
  17. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  18. Context context = createPackageContext(&quot;io.ionic.starter&quot;, 0);
  19. AssetManager assetManager = context.getAssets();
  20. InputStream is = assetManager.open(&quot;pack1&quot;);
  21. }
  22. }
  23. When building the App I get this error:
  24. &gt; Task :app:compileDebugJavaWithJavac FAILED
  25. C:\Users\LUM\appAsset\platforms\android\app\src\main\java\cordova\plugin\hello\hello\hello.java:22: error: cannot find symbol
  26. Context context = createPackageContext(&quot;io.ionic.starter&quot;, 0);
  27. ^
  28. symbol: method createPackageContext(String,int)
  29. location: class hello
  30. Note: Some input files use or override a deprecated API.
  31. Note: Recompile with -Xlint:deprecation for details.
  32. 1 error
  33. I am very new to app development and all possible solutions that I found on the Internet created different Errors and while going further away from the three lines of code you can see in the first linked article. For example adding `this.cordova.getActivity()` and call `createPackageContext(&quot;io.ionic.starter&quot;, 0);` on that. That created even more errors. Same with other fixing tries.
  34. I get it that it&#39;s normal to get Errors even after fixing others, but it would really help me out if you can help me out with what I am fundamentally doing wrong with the code.
  35. Android SDK Tools : 26.1.1
  36. NodeJS : v12.18.3
  37. npm : 6.14.6
  38. OS : Windows 10
  39. Gradle : 6.6.1
  40. Ionic Framework : @ionic/angular 5.3.3
  41. Cordova Platforms : android 9.0.0
  42. [1]: https://developer.android.com/guide/playcore/asset-delivery/integrate-java#install-time-delivery
  43. </details>
  44. # 答案1
  45. **得分**: 1
  46. [@Stultuske](https://stackoverflow.com/users/2636929/stultuske) 是正确的 - `createPackageContext()` 是 `android.content.Context` 类的一个实例方法,因此您需要在现有的类实例上调用它。
  47. 您可以使用应用程序上下文实例;尝试以下代码:
  48. ```java
  49. Context context = this.cordova
  50. .getActivity()
  51. .getApplicationContext()
  52. .createPackageContext("io.ionic.starter", 0);
英文:

@Stultuske is right - createPackageContext() is an instance method on the android.content.Context class so you need to call it on an existing class instance.
You can use the application context instance; try this:

  1. Context context = this.cordova
  2. .getActivity()
  3. .getApplicationContext()
  4. .createPackageContext(&quot;io.ionic.starter&quot;, 0);

huangapple
  • 本文由 发表于 2020年10月2日 19:52:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64171075.html
匿名

发表评论

匿名网友

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

确定