title: Javascript Interface, error "Uncaught Error: Error calling method on NPObject"
body: <p>In my Android application, I am using the WebAppInterface to bridge from Javascript to Java.</p>
  
  <p>My .html file includes some JS that runs "Android.canPlay();" at certain times.</p>
  
  <p>In my app, the function "canPlay()" fires a variable listener.</p>
  
  <p>When the variable listener fires, if conditions are met, some files are renamed (temporary files, that were downloaded with a .tmp additional extension, including the .html file (so it is .html.tmp, with the original .html existing alongside it))</p>
  
  <p>After the renaming happens, the Web View is reloaded (I have tried using both a function and just ".loadUrl()" - both give errors)</p>
  
  <p>When time comes to reload the html file in the webview, I get the error:</p>
  
  <blockquote>
    <p>E/Web Console﹕ Uncaught Error: Error calling method on NPObject. at
    file:////mnt/sdcard/Download/qwerty/playlists/29/2/index.html:206</p>
  </blockquote>
  
  <p>Line 206 is the "Android.canPlay()" call.</p>
  
  <p>Things to note - If I don't rename the files before reloading the webview and instead, rename then from a background service that runs, I don't have this error (although I do get one about running stuff on the WebViewCoreThread)</p>
  
  <p>Some code:</p>
  
  <p>WebAppInterface</p>
  
  <pre><code>public class WebAppInterface {
      final Context mContext;
  
      WebAppInterface(Context c) {
          mContext = c;
      }
  
      @JavascriptInterface
      public void canPlay()
      {
          mPlaylistReady.changeState(true);
      }
  }
  </code></pre>
  
  <p>Variable Listener (gets fired via a listener class) (some bits of code removed e.g. sharedprefences for file locations)</p>
  
  <pre><code>@Override
  public void onStateChange(boolean state) {
  
      if (state) { // true
              // this finds all the files and removes the .tmp extension
              fileRenamer.removeTmp(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/qwerty/playlists/"));
  
              // reloads the webview
              WebViewActivity.this.runOnUiThread(new Runnable() {
                  public void run() {
                      webView.loadUrl("file:///" + htmlFile.getAbsolutePath());
                  }
              });
  
          }
      }
  }
  </code></pre>
  
  <p>FileRenamer class</p>
  
  <pre><code>public class FileRenamer {
  
      public void removeTmp(File directory)
      {
          File dir = directory;
          if(dir.isDirectory()) {
              for(File child : dir.listFiles()) {
                  String name = child.getName();
                  String ext = name.substring(name.lastIndexOf('.'));
                  if(ext.equals(".tmp")) {
                      final int lastPeriodPos = name.lastIndexOf('.');
                      File renamed = new File(dir, name.substring(0, lastPeriodPos));
                      child.renameTo(renamed);
                  }
              }
          }
      }
  
  }
  </code></pre>