4

I'm trying to set FLAG_SHOW_WHEN_LOCKED in my phonegap app, but only when a certain page is shown. To do so, I have a Java plugin extending from CordovaPlugin with the following code in the execute method:

if (action.equals("showWhenLocked")) {
    boolean showWhenLocked = args.getBoolean(0);

    if (showWhenLocked) {
        this.cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    } else {
        this.cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    }

    callbackContext.success();
    return true;
}

It gets called with cordova.exec(null, null, 'MyPluginClass', 'showWhenLocked', [myVar]), but on execution I get the error

Uncaught Error: Error calling method on NPObject. at file:///android_asset/www/cordova-2.2.0.js:984

Any ideas what's causing this/what I'm doing wrong and how to fix it? If I set the flag upon creating the activity it works just fine.

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100

3 Answers3

3

I have found already that this kind of error can be caused by calls that require threads that are not available. Your use of getWindow() tell me that this is even more likely. You are most likely accessing information locked by another thread, like the UI thread. Check out the cordova documentation in the section about threading in the UI thread.

  • Thank you. I had thought about a threading problem before, but figured that I'm not using the UI thread so it should be okay --a little naive. I'll give this a try and let you know. It could take a few days, though. – Ingo Bürk Jan 30 '13 at 06:56
  • 1
    I've actually seen this message a few times in the last few days, with phonegap plugins. It also happens when a function you are using throws an exception that isn't caught. The best way to find out what is really going on is to put your call in a try, catch and use `Log.d("TAG",e.toString());` to get an error report. In one case I found that my try-catch wasn't catching, so I used `Error` instead of `Exception` and found my problem. – rickstockham Jan 31 '13 at 00:15
  • I don't know how I didn't think of catching the error before. I'll try that too, thanks. Again, it might take a while and I apologize for this. I will definitely come back here and (if it worked) accept the answer etc. – Ingo Bürk Jan 31 '13 at 08:10
  • I tried catching the error now and it's a CalledFromWrongThreadException ("Only the original thread that created a view hierarchy can touch its views."). Running it on the UI thread doesn't throw this error anymore, however I will still have to check whether it's actually working sometime soon. – Ingo Bürk Feb 03 '13 at 20:37
  • Well guys, I promised I wouldn't forget -- and I finally had the time to finish this. Running it on the UI thread finally did the trick. Thanks! – Ingo Bürk Feb 13 '13 at 20:03
  • Thanks for the last vote, it helps out with someone beginning on here to finally be able to vote on answers as well. I appreciate you coming back to give the final yes. – rickstockham Feb 14 '13 at 16:52
0

Using:

callbackContext.success();

will explicitly call the success callback but in your case the success callback is null so that is why you get the error.

Try using:

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));

that way the exec call returns but the JS will check to see if there is a success callback before trying to call it.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Doesn't change anything, unfortunately. Also, I've used `callbackContext.success();` for another plugin that I call without a callback and no problems there. I changed it in both cases now, but as mentioned -- no effect. – Ingo Bürk Jan 09 '13 at 11:20
  • If I comment out the `addFlags` call I don't get any errors. So I guess something's wrong with that call? – Ingo Bürk Jan 09 '13 at 11:32
0

Adding android-support-v4.jar to project build path and checking it's entry in Order and Export (tab) of Build path works for me . - Thanks, Prodeveloper

prodeveloper
  • 943
  • 14
  • 22