简体   繁体   中英

@JavascriptInterface cannot be resolved

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 @JavascriptInterface before any function you wish to be accessible by the WebView and that without it, Jelly Bean won't recognize the function.

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

@JavascriptInterface cannot be resolved to a type

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:

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();
    }

}

@JavascriptInterface is introduced in JellyBean.

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

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

The APK will work on older versions of android.

You need to add this:

import android.webkit.JavascriptInterface;

Check the sample here:

Steps for users of Android Studio:

  1. Check that in build.gradle the minSdkVersion is 17 or above
  2. Add import android.webkit.JavascriptInterface;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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