Android Question How to convert the following code from reflection to JavaObject ?

avacondios

Active Member
Licensed User
Longtime User
Hi,

I want to convert the following code from Reflection to javaObject.

B4X:
Dim mReflector As Reflector
mReflector.Target = mReflector.GetActivity
mReflector.Target = mReflector.RunMethod("getWindowManager")
mReflector.Target = mReflector.RunMethod("getDefaultDisplay")

the problem is , how to store the activity on javaObject variable.
It is not working, if I use the following stamement : Dim mJO as JavaObject = Activity. For the rest of commands, I am receiving an exception that getWindowManager and getDefaultDisplay not found.

Thanks for your support
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add these two subs to your code:
B4X:
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

B4X:
Dim manager As JavaObject = GetContext.RunMethod("getWindowManager", null)
...
 
Upvote 0

avacondios

Active Member
Licensed User
Longtime User
Thanks Erel....

How can I get the active context from a service ? I have a service that I want to get the properties of default display like height, width, etc. Is it possible to get the active context without to execute the GetContext sub from the activity ?
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi all,
I have the code using Reflector already as below,I would like to convert to use JavaObject. I tried more than hours,but not finished. Please help me.
B4X:
Private Sub GetResources As Object
   
    Dim r1 As Reflector
    r1.Target = r1.GetContext
    Return r1.RunMethod("getResources")
EndSub


My tried is below which is wrong.
B4X:
Private Sub GetResources As Object
    Dim jo As JavaObject
     jo.GetField("context") 
     Return jo.RunMethod("getResources",Null)
EndSub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code is wrong as jo is not pointing to any object.

Add these two subs to your code:
B4X:
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

And then you should use:
B4X:
Private Sub GetResources As JavaObject
 Return GetContext.RunMethodJO("getResources", Null)
End Sub
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Erel,or Experts

1. What is "Me"? in the line?
B4X:
      Dim cls AsString = Me
2. In the same case which always concerned I can't use JavaObject library instead of below
B4X:
Private Sub GetDrawable(id As Int) As Object
    Dim r2 As Reflector
    r2.Target = GetResources    '<--- without it the reflector object will be null
    Return r2.RunMethod2("getDrawable", id, "java.lang.int")

My tried is wrong as below
B4X:
Private Sub GetDrawable(id As Int) As Object
  Dim jo2 As JavaObject=GetResources
        Return jo2.RunMethod("getDrawable",id)
EndSub


P.S. I do it in order to add in my website issue at 55 ,Please understand me and don't feel annoyed me. :(
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Erel,
After I test my smartphone, I 've error "Program paused on line 68 jo.InitializeStatic(cls)" Do I have error in coding?
 

Attachments

  • Test.zip
    9 KB · Views: 186
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi all,
My computer's harddisk is breaken down ,I must earn money to buy a new one. I'm so late. This is my error message from the log. B4A has no copy button,I capture as picture instead.

P.S. The sourcecode at post #9 from Erel's tutorial,I had changed a little to use JavaObject lib instead of Reflection lib.
 

Attachments

  • MyError.png
    MyError.png
    34.7 KB · Views: 179
  • errorAVD.png
    errorAVD.png
    110.6 KB · Views: 189
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
LogCat connected to: emulator-5554
--------- beginning of /dev/log/system

--------- beginning of /dev/log/main

** Activity (main) Create, isFirst = true **

java.lang.ClassNotFoundException: Button): Left=0, Top=0, Width=48, Height=48, Text=, et=(EditText) Not initialized, main=null

, vr=anywheresoftware$b4a$phone$Phone$VoiceRecognition@b221ec10]

at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:266)

at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:52)
at b4a.example.speakbutton._getba(speakbutton.java:113)
at b4a.example.speakbutton._getcontext(speakbutton.java:135)
at b4a.example.speakbutton._getresources(speakbutton.java:181)
at b4a.example.speakbutton._loadresourcedrawable(speakbutton.java:231)
at b4a.example.speakbutton._designercreateview(speakbutton.java:80)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:57)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:155)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:208)
at b4a.example.main._activity_create(main.java:321)
at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:525)

at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassNotFoundException: Button): Left=0, Top=0, Width=48, Height=48, Text=, et=(EditText) Not initialized, main=null

, vr=anywheresoftware$b4a$phone$Phone$VoiceRecognition@b221ec10]
** Activity (main) Create, isFirst = true **

java.lang.ClassNotFoundException: Button): Left=0, Top=0, Width=48, Height=48, Text=, et=(EditText) Not initialized, main=null

, vr=anywheresoftware$b4a$phone$Phone$VoiceRecognition@b221ebe8]
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:266)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:52)
at b4a.example.speakbutton._getba(speakbutton.java:113)
at b4a.example.speakbutton._getcontext(speakbutton.java:135)
at b4a.example.speakbutton._getresources(speakbutton.java:181)
at b4a.example.speakbutton._loadresourcedrawable(speakbutton.java:231)
at b4a.example.speakbutton._designercreateview(speakbutton.java:80)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:57)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:155)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:208)
at b4a.example.main._activity_create(main.java:321)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Me is refered as button?
 
Last edited:
Upvote 0
Top