Android & PhoneGap -- Error calling method on NPObject - java

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.

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.

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.

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

Related

Why Android Studio debugger doesn't stop on code that is called inside runOnUiThread

I found this bug on with the following code:
(it.context.get() as? Activity)?.runOnUiThread(Runnable {
it.weakRefIOnDataRefreshed.get()?.onDataRefreshed(refreshedItemList)
})
The code above is inside a run method that runs in a non UI Thread. I want to call a method in fragment with the refreshedItemList as argument. I want to debug the onDataRefreshed method (which is inside a Fragment), and I'm putting inside this function a break point but nothing happens. I also put a Log method to make sure that the code is running and the Log method prints indeed. What may be the problem that the debugger doesn't stop on the line which I have marked?
In your class where that background thread works define a Handler object and use post method to update UI e.g. (Java).
// Define handler object
Handler handler = new Handler(Looper.getMainLooper);
...
...
// Here ise where you want to update your UI
handler.post(() -> {
// Your code with which you want to update the UI
}

W/System: A resource failed to call release

When using the console of AndroidStudio on my app it shows:
W/System: A resource failed to call release.
Sometimes it is said multiple times. I know what it means but I've checked the almost 2k lines of code multiple times but I'm clueless what I'm missing to close/release.
Is there any way to expand on this information from the console? or how would you do to target what the resource is or when it fails to close? Any ideas are welcome. Thanks.
The answer from #guest works, but you can achieve the exact same thing without resorting to reflection using Strict Mode. Specifically, something like:
StrictMode.setVmPolicy(new VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyLog()
.build());
In general, strict mode can do much more for you though (see link above to doc), and all you need to do for a default setup is:
StrictMode.enableDefaults(); # <-- This includes warning on leaked closeables
To enable strict mode "as soon as possible" you can add either of the above code options to the constructor of your application class, e.g.:
public class MyApp extends Application {
public MyApp() {
if(BuildConfig.DEBUG)
StrictMode.enableDefaults();
}
}
Note that in addition to just creating the class above, you need to tell Android that you have created a custom application class in the AndroidManifest.xml (so that an instance of it gets created when your application process starts, instead of Android creating the default Application class). You need to add/modify the android:name attribute of the <application> tag to point to the fully resolved package path of your custom application class (MyApp in this case):
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:name="com.example.app.MyApp" <-- IMPORTANT PART: ADAPT FOR YOUR ACTUAL PROJECT
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
This message comes from dalvik.system.CloseGuard. When debugging, you can set it up to create stack traces as you create resources, so that you can track down what objects aren't being closed.
It's not part of the framework API, so I'm using reflection to turn that on:
try {
Class.forName("dalvik.system.CloseGuard")
.getMethod("setEnabled", boolean.class)
.invoke(null, true);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
more info: https://wh0.github.io/2020/08/12/closeguard.html
I don't think that you can get any more information out of Logcat.
The memory view of the Android Profiler is probably a good place to start. Looking at it while using your app should give you an idea of what actions cause memory to be allocated and not released.
You can also select sections from the timeline and drill down to specific allocations by
class.
Alternatively LeakCanary is a great library to detect memory leaks.
Fixed it by removing a function call that called itself through another function thereby making an infinite loop of calls to itself
Late answer, but it may be useful to someone else:
I faced the same error, but I forgot I had my VPN running in the background. Disconnecting the VPN did the trick for me.
This is to say it may be due to resources unrelated to your app or IDE you may want to check, like an antivirus, VPN, etc.

JavaFX: How to check if page available or not

In my project I use simple JavaFX browser, that works in background and do some stuff without displaying it.
More precisely it submitted some form to one online page.
So, I ran into a problem: when this page doesn't available, I can't figure it out from my Java code, it looks like form wasn't submitted and clicks on Submit button do nothing, but in Chrome for example I see that the page isn't available.
So, is there an option to check from Java code if page is available?
Thanks in advance and sorry for bad English.
So, I found an answer.
There is Worker class in JavaFX that associates with WebEngine and it has a field with State type.
State is enum that has 6 options:
READY
SCHEDULED
RUNNING
SUCCEEDED
CANCELLED
FAILED
So State.FAILED can be used for handling errors.
For example, something like that (we'll assume we have WebEngine instance):
webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
if (newState == Worker.State.FAILED) {
doOnError();
return;
}
doOnSuccess();
});
webEndgine.load("example.com");
So, every time state is changed ObservableValue#changed will be called with new State value as one of parameteres and if state become FAILED we do some error processing.

Android Libgdx Fatal signal 11(SIGSEGV) ,code 1, fault addr 0x0

I know this is an error with accessing memory outside the readspace but i have absolutely no idea how to fix this. I'm new to android, so i don't exactly know how to print out a more detailed error list from logcat in eclipse. I've tried everything from disposing literally everything, to calling System.gc to setting all my variables to null. However, whenever i switch screens the fatal signal occurs. I just need someone to tell me what exactly is going on or how i could get more details about the error.
I had the same error, what solved it was to make sure i'm on the UI thread, like this:
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
// Your crashing code here
}
});
In my case i received same error when i try to create a new body and attach it's fixture, from beginContact (inside Contact Listener). After i moved outside Contact Listener my body creation everything was ok. Probably some conflict appears in Fixture createFixture (FixtureDef def) because according to manual: Contacts are not created until the next time step.

Trying to use the Netbeans ProgressBar to show long running tasks, having problems with dependancies and libraries

I have an application built on top of NetBeans. We have some long running jobs that I'd like to keep running in the background, but allow the user to see progress on the bar on the lower right.
E.G:
I can't seem to access it from my code.
Initially I didn't have this library
org.netbeans.api.progress.ProgressHandle;
org.netbeans.api.progress.ProgressHandleFactory;
I had to go out and hunt down the JAR file. That doesn't make whole lot of sense to me, I figure it should be available.
This creates an error when I try to call the ProgressHandle into effect, I get this error
java.lang.ClassNotFoundException: org.openide.awt.StatusLineElementProvider …
Followed by a stack trace. Obviously I don't have all the packages necessary to operate this.
What the big question is then, what am I missing as far as accessing these NetBeans libraries correctly?
Thanks,
Here's the code when I'm trying to call the progressbar into action
`
ProgressHandle progr;
if (thread == null) {
thread = new Thread() {
#Override
public void run() {
progr.start();
progr.progress("Sending backup to remote server.");
… //Some code that sends a backup
progr.finish();
`
I'll be rewriting this question a few times, until I think it's clear, I'm open to input
Add a module dependency on Progress API. Right click on your module > properties.Select Libraries from the left panel. Click Add to open up the module dependency dialog. Select Progress API and click OK. Now you have the dependency on Progress API and you can use it as
ProgressHandle ph = ProgressHandleFactory.createSystemHandle("My Task");
ph.start(100);
Edit:
Also u dont have to add any jar files.. The Progress API module dependency will take care of that

Categories

Resources