javascript @JavascriptInterface 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14353026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:25:40  来源:igfitidea点击:

@JavascriptInterface cannot be resolved

javascriptandroidwebviewandroid-4.2-jelly-bean

提问by user1982687

I'm working with Android to make a webpage interact with my app. As such, I've gone through the documentation and tutorials and ended up coming up with this site. In it, the developers list that you should include @JavascriptInterfacebefore any function you wish to be accessible by the WebView and that without it, Jelly Bean won't recognize the function.

我正在使用 Android 使网页与我的应用程序交互。因此,我已经浏览了文档和教程,并最终想出了这个站点。在其中,开发人员列出了您应该@JavascriptInterface在您希望 WebView 访问的任何功能之前包含的列表,如果没有它,Jelly Bean 将无法识别该功能。

My problem is that when I put that in, I get an error saying:

我的问题是,当我把它放进去时,我收到一条错误消息:

@JavascriptInterface cannot be resolved to a type

@JavascriptInterface 无法解析为类型

Without it, my code compiles and works fine, but I want Jelly Bean compatibility. I'm currently working on Android 1.6 as a base, so does it just not have @JavascriptInterface? Is that a Jelly Bean specific thing, meaning I'll have to make a program specifically for Jelly Bean? Any help would be greatly appreciated. Here is my complete interface class:

没有它,我的代码可以编译并运行良好,但我想要 Jelly Bean 兼容性。我目前正在使用 Android 1.6 作为基础,所以它只是没有@JavascriptInterface吗?那是果冻豆特有的东西,这意味着我必须专门为果冻豆制作一个程序吗?任何帮助将不胜感激。这是我完整的接口类:

import android.content.Context;

public class WebAppInterface{

    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    //needed for jelly bean
    @JavascriptInterface
    public void turnOff() {
        MyAlarmPopup.turnOff();
    }

}

回答by Tarak

@JavascriptInterfaceis introduced in JellyBean.

@JavascriptInterface在 JellyBean 中引入。

You need to set the Project Build Target to API Level 17

您需要将项目构建目标设置为 API 级别 17

From Eclipse IDE, go to Project->Properties. Select Android and set the target

从 Eclipse IDE,转到 Project->Properties. 选择Android并设置目标

The APK will work on older versions of android.

APK 将适用于旧版本的 android。

回答by willandlily

You need to add this:

你需要添加这个:

import android.webkit.JavascriptInterface;

Check the sample here:

在此处检查示例:

回答by Balazs F.

Steps for users of Android Studio:

Android Studio 用户的步骤:

  1. Check that in build.gradle the minSdkVersion is 17 or above
  2. Add import android.webkit.JavascriptInterface;
  1. 检查 build.gradle 中的 minSdkVersion 是否为 17 或更高
  2. 添加 import android.webkit.JavascriptInterface;