title: Phonegap: calling a java function from Javascript
body: <p>I want to call one of my Java functions in Javascript and get its result. In order to do that I followed <a href="https://github.com/phonegap/phonegap/wiki/How-to-show-and-hide-soft-keyboard-in-Android" rel="nofollow noreferrer">this tutorial</a> and <a href="https://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript">this question</a>. I followed them step by step and I still get this error</p>
  
  <blockquote>
    <p>Cannot call method 'showKeyBoard' of undefined</p>
  </blockquote>
  
  <p>Here is my java class:</p>
  
  <pre><code>package keyboard;
  import org.apache.cordova.DroidGap;
  import android.content.Context;
  import android.view.inputmethod.InputMethodManager;
  import android.webkit.WebView;
  
  public class KeyBoard {
    private WebView mAppView;
    private DroidGap mGap;
    public KeyBoard(DroidGap gap, WebView view) {
      mAppView = view;
      mGap = gap;
    }
    public void showKeyBoard() {
      InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
      // only will trigger it if no physical keyboard is open
      mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
      ((InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mAppView, 0);
    }
    public void hideKeyBoard() {
      InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
      mgr.hideSoftInputFromWindow(mAppView.getWindowToken(), 0);
    }
  }
  </code></pre>
  
  <p>Here is my Main class:</p>
  
  <pre><code>package com.example.helloworld;
  import keyboard.KeyBoard;
  import android.os.Bundle;
  import org.apache.cordova.*;
  import android.view.Menu;
  import QR.*;
  
  public class MainActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      super.init();
      KeyBoard keyboard = new KeyBoard(this, appView);
      appView.addJavascriptInterface(keyboard, "KeyBoard");
      super.loadUrl("file:///android_asset/www/index.html");
    }
  
    @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
      }
    }
  </code></pre>
  
  <p>And I call it in Javascript like this:</p>
  
  <pre><code>(function(){
    window.KeyBoard.showKeyBoard();
  })();
  </code></pre>
  
  <p>Is there anything that I haven't done or am missing? As I said I get this error:</p>
  
  <blockquote>
    <p>Cannot call method 'showKeyBoard' of undefined</p>
  </blockquote>