Android Question Inline Java Error for accessing third party SDK

cambopad

Active Member
Licensed User
Longtime User
Dear all,

I found in line java is fun to play with, so I tried to access a third party SDK using the inline java feature.

But I have one problem with the word "this" in the original java codes of the SDK I am testing.

This is the original java codes:

B4X:
private void destroyVideoAd() {
         // (Optional) Cancel listener for earning currency points. If register listener in onCreate, remember to cancel
        // it.
        VideoAdManager.getInstance(this).unRegisterRewards(this);
       
        // Remember to invoke the below code on application exit, to tell SDK that the application is closed, which can
        // make SDK release some resource about video.
        VideoAdManager.getInstance(this).onDestroy();
    }

And when I compile, I got this error:
B4X:
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.    (0.00s)
Generating R file.    (0.24s)
Compiling generated Java code.    Error
javac 1.7.0_79
src\com\trialpay\example\main.java:649: error: method unRegisterRewards in class VideoAdManager cannot be applied to given types;
        VideoAdManager.getInstance(this).unRegisterRewards(this);
                                        ^
  required: VideoRewardsListener
  found: main
  reason: actual argument main cannot be converted to VideoRewardsListener by method invocation conversion
1 error

I wonder what's wrong with it, so I just changed the word "this" to "null" like this:

B4X:
VideoAdManager.getInstance(this).unRegisterRewards(null);

Then I could compile the project without any errors. I am really doubt about this as I believe changing the word "this" to "null" is not good solution and can cause some errors at anytime.

Can anyone explain me something about this or suggest a better solution?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Judging by the fact that the Java method you posted is private, I'm guessing that that Java method was originally found inside a Java class. In that case, "this" refers to an instance of that Java class. However, if you extract that method from its class context and use it in an Activity, "this" will now refer to that Activity. The error javac gives you seems to support this: "this" is supposed to be of type VideoRewardsListener (this is probably the class in which that method first appeared) but it is instead referring to the main Activity. If you continue to use that method in an Activity, you'll need to get an instance of VideoRewardsListener to pass as an argument instead of "this".
 
Upvote 0

cambopad

Active Member
Licensed User
Longtime User
Thanks you @Roycefer! You guess is absolutely true!

Now I found that code that you said:

B4X:
    public interface VideoRewardsListener {
       
   /**
     * Currency reward return,
     * this call-back will proceed in UI thread, can interactive directly with UI
    **/
    public void onVideoRewards(int reward);
   
}

But I don't know whether it is possible to use in Inline Java?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
This code is just the interface. You'll need to find a class that implements that interface and get an instance of that class. I'm guessing that the class VideoAdManager implements this interface but without looking at the code, this is just a wild guess.

I think you might be better off leaving the class code intact (that is, put it all in the in-line Java) and find some code samples in the SDK that do what you want and copy-paste those code samples into the in-line Java section as Java functions. Extracting private methods from a class will be problematic.

If you find this solution too unwieldy, then this SDK might be better suited to a standard wrapper library.
 
Upvote 0

cambopad

Active Member
Licensed User
Longtime User
Thanks you for your response! Actually, I am testing this SDK:
Reward Video Ad:
https://www.adxmi.com/static/doc/an...SDK_Video_Document_International_Version.html

Offerwall
https://www.adxmi.com/static/doc/an...OfferWall_Document_International_Version.html

With Inline Java:
1. I could successfully show their offerwall
2. I could successfully show their reward video

My big problem is that I cannot track the "reward" (as you can see in
VideoRewardsListener)
after the user complete watching the reward video (Points or Credits will be rewarded to users after they completed watching the video).

I almost reach the successful stage in using Inline Java to use this SDK, but I am stuck at the end :(

I am also looking for those who can make the wrapper for this SDK and willing to pay for their work!
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Looking at this part: https://www.adxmi.com/static/doc/an...ersion.html#acquire-by-implementing-interface , they say that you have to implement the interface yourself. It says it should be a View or an Activity class but I'm not sure I see why it has to be. Try something like this:
B4X:
public class VRLClass implements VideoRewardsListener
{
    public void onVideoRewards(int reward)
    {
         processBA.raiseEvent2(null, true, "vrlclass_videorewards", false, reward);
    }
}

This will raise an event in your B4A code called vrlclass_videorewards(reward As Int) when the onVideoRewards method is invoked. To register and unregister it, call this code:
B4X:
public VRLClass v = new VRLClass();   //This should be "global" within your in-line Java

public void RegisterVRL()    //This will register the VRLClass instance created above. You can call this function using JavaObject
{
    VideoAdManager.getInstance(this.getApplicationContext()).registerRewards(v);
}

public void UnregisterVRL() //This will unregister the VRLClass instance created above. You can call this function using JavaObject 
{
    VideoAdManager.getInstance(this.getApplicationContext()).unRegisterRewards(v);
}

Obviously, this is all wildly untested.
 
Upvote 0

cambopad

Active Member
Licensed User
Longtime User
OMG! You are my life saver! My friend <3

Thanks you so much for taking times to read that and provide the solution!

Obviously, this is all wildly untested.
Your provided codes are perfectly working, and now I can track the reward points after the users complete watching the videos! :)
Your Java knowledge must be great! You taught me a good lesson for this :) So I think it might be also possible for other SDKs.

Thanks you my friend!
 
Upvote 0
Top