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

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

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

问题

插件的 Java 文件:

package cordova.plugin.hello;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

// 我的导入
import android.content.res.AssetManager;
import android.content.Context;
import java.io.InputStream; 

public class hello extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Context context = createPackageContext("io.ionic.starter", 0);
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open("pack1");
    }
}

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

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


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

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.

Plugins Java file:

    package cordova.plugin.hello;

    import org.apache.cordova.CordovaPlugin;
    import org.apache.cordova.CallbackContext;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    //My Imports
    import android.content.res.AssetManager;
    import android.content.Context;
    import java.io.InputStream; 

    public class hello extends CordovaPlugin {
        @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
            Context context = createPackageContext(&quot;io.ionic.starter&quot;, 0);
            AssetManager assetManager = context.getAssets();
            InputStream is = assetManager.open(&quot;pack1&quot;);
        }
    }
When building the App I get this error:

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

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.

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.



    Android SDK Tools : 26.1.1
    NodeJS            : v12.18.3
    npm               : 6.14.6
    OS                : Windows 10
    Gradle            : 6.6.1
    Ionic Framework   : @ionic/angular 5.3.3
    Cordova Platforms : android 9.0.0

  [1]: https://developer.android.com/guide/playcore/asset-delivery/integrate-java#install-time-delivery

</details>


# 答案1
**得分**: 1

[@Stultuske](https://stackoverflow.com/users/2636929/stultuske) 是正确的 - `createPackageContext()` 是 `android.content.Context` 类的一个实例方法,因此您需要在现有的类实例上调用它。
您可以使用应用程序上下文实例;尝试以下代码:

```java
Context context = this.cordova
        .getActivity()
        .getApplicationContext()
        .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:

Context context = this.cordova
        .getActivity()
        .getApplicationContext()
        .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:

确定