1

I am developing an Android app with jQuery Mobile and PhoneGap. This is my activity:

public class xxx extends DroidGap {
  /** Called when the activity is first created. */
  private String FILENAME = "";
  private static final String HTML_ROOT = "file:///android_asset/www/";
  private Handler handler = null;
  private static WebView webView = null;
  public MyTeam team = null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    handler = new Handler();
    FILENAME = getString(R.string.fileName);
    //parsing data from file
    String json = readFile();
    team = parseJson(json);
    appView.addJavascriptInterface(this, "MyTeam");
    super.loadUrl("file:///android_asset/www/menu.html");
  }
  public String HW(){
    return team.getTeamName();
  }

And this is my Javascript code in menu.html:

$(document).ready(function(){
  alert(MyTeam.HW()); // work perfectly
  alert("name "+MyTeam.team.getTeamName()); //doesn't work!
});

So the problem is:

  • I can access the HW function that returns me team.getTeamName();
  • I don't have access to the team variable name in my activity because MyTeam.team.getTeamName() doesn't work.

It seems that I can access only functions but not directly public variables.

What I am doing wrong?

Thanks.

4

1 回答 1

2

You can't access the Java objects fields directly. It's right there in the documentation of addJavaScriptInterface. Also, if you are using PhoneGap then you should just write a plugin and let us worry about the cases where addJavaScriptInterface falls down.

于 2012-11-26T04:02:09.063 回答