简体   繁体   English

为什么我不能在cordova cusotm插件中调用导入的android平台api类的Java方法

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

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.我正在使用 Ionic 创建一个应用程序,为了加载与 Play Asset Delivery 一起使用的资产,我需要通过创建一个cordova插件来访问android本机代码。 I need to add those three lines of code .我需要添加这三行代码 In order to achieve this I imported the Android Platform APIs that contain those methods and classes.为了实现这一点,我导入了包含这些方法和类的 Android 平台 API。

Plugins Java file:插件 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;

//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("io.ionic.starter", 0);
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open("pack1");
    }
}

When building the App I get this error:构建应用程序时,我收到此错误:

> 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

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.我对应用程序开发非常陌生,我在 Internet 上找到的所有可能的解决方案都创建了不同的错误,同时远离您可以在第一篇链接文章中看到的三行代码。 For example adding this.cordova.getActivity() and call createPackageContext("io.ionic.starter", 0);例如添加this.cordova.getActivity()并调用createPackageContext("io.ionic.starter", 0); on that.在那。 That created even more errors.这造成了更多的错误。 Same with other fixing tries.与其他修复尝试相同。

I get it that it'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

@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. @Stultuske是对的 - createPackageContext()android.content.Context类上的一个实例方法,因此您需要在现有类实例上调用它。 You can use the application context instance;您可以使用应用程序上下文实例; try this:尝试这个:

Context context = this.cordova
        .getActivity()
        .getApplicationContext()
        .createPackageContext("io.ionic.starter", 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM