Android Question How to achieve this in B4A using Reflection or javaobject?

Rajesh kannan MJ

Member
Licensed User
Longtime User
Hi,

I need to achieve this java code into B4A

xWalkWebView = new XWalkView(this.getApplicationContext(), this);
xWalkWebView.loadUrl("file:///android_asset/www/index.html");

You can see the overall code here https://gist.github.com/butelo/9365587

I am able to get the "context" with reflector getcontext method. But I don't know how to pass "this" to the code.

Thanks for the help in advance,
 

Roycefer

Well-Known Member
Licensed User
Longtime User
In most cases, "this" in Java will translate to "Me" in B4X. They both refer to the object that owns the current scope of execution. If you are writing this code in a B4X class, this/Me will refer to the current instance of that class when that instance is being executed. If you are writing it in an Activity, it will refer to that Activity.
 
Upvote 0

Rajesh kannan MJ

Member
Licensed User
Longtime User
This is the code I have written,

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#AdditionalJar: xwalk_core_library_java
Sub Globals
End Sub
Sub Process_globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Dim R As Reflector
    Dim thisme As JavaObject = Me
    Dim arrParamsobj As JavaObject
    Dim arrParams(2) As Object
    arrParams(0) = R.GetContext
    arrParams(1) = R.Target
    arrParamsobj.InitializeNewInstance("org.xwalk.core.XWalkView", Array(GetContext,thisme))
    'Activity.AddView(arrParamsobj,0,0,100%x,100%y)
End Sub

Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub


But getting error "constructor not found

B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 342)
java.lang.RuntimeException: Constructor not found.
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:93)
    at b4a.example.main._activity_create(main.java:342)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at b4a.example.main.afterFirstLayout(main.java:100)
    at b4a.example.main.access$100(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Constructor not found.

Can some one tell me what exactly I am missing here in the code,

Thank you,
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
The parameters for the Constructor of XWalkView are wrong. It needs a context and the Activity. So it should be something like this:

B4X:
arrParamsobj.InitializeNewInstance("org.xwalk.core.XWalkView", Array as Object(R.GetContext, R.GetActivity))
 
Upvote 0

Rajesh kannan MJ

Member
Licensed User
Longtime User
@corwin42 I tried your option now. Thanks for your input, But I am sorry it is giving the following error,

Can you please tell me whether it is possible to implement it?

B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 339)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:89)
    at b4a.example.main._activity_create(main.java:339)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at b4a.example.main.afterFirstLayout(main.java:100)
    at b4a.example.main.access$100(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: junit.framework.AssertionFailedError: Please have your activity extend XWalkActivity for shared mode
    at junit.framework.Assert.fail(Assert.java:47)
    at org.xwalk.core.XWalkCoreWrapper.initEmbeddedMode(XWalkCoreWrapper.java:182)
    at org.xwalk.core.XWalkView.reflectionInit(XWalkView.java:731)
    at org.xwalk.core.XWalkView.<init>(XWalkView.java:237)
    ... 19 more
java.lang.reflect.InvocationTargetException
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
The problem is explained in this line:

Caused by: junit.framework.AssertionFailedError: Please have your activity extend XWalkActivity for shared mode

You have to extend the Activity from XWalkActivity. This can be done with the #Extends: property in B4A.

For your question if it is possible to use this library: I don't have looked very much at it but it should be possible. Problem is that you will need some java knowledge and I think it will be far better to write a wrapper library in Java for it.

I don't know what you want to do with it. If you only need 2-3 methods from the library it will be ok to try it with Reflection and JavaObject. If it is more then I really would suggest to write a wrapper library for it.
 
Upvote 0

Rajesh kannan MJ

Member
Licensed User
Longtime User
@corwin42 I once again thank you for giving your input. I have limited knowledge in Java. However, I will try the #Extends: property and I will learn it.

Actually, the main thing is that I want to use this xwalk library just to load a html page. So, other than load method, I will not use any other methods.

loadurl is the only one I need. Corwin, I am really so happy to see B4A does great in using JAR. I will be so happy if only I am able to access this library. Because, many users are expecting it.

You could have seen "wishes" to use Webgl and crosswalk for loading html 5 pages. Because, this library is 20x runs faster ,

Thank you Corwin,
 
Upvote 0
Top