Android Question Passing an Activity as a parameter problem

techgreyeye

Member
Licensed User
Longtime User
Hi All,

I'm trying to add camera based barcode scanning to one of my apps, based on this example (https://www.b4x.com/android/forum/t...on-google-play-services-vision.89705/#content).

I've created an activity to do the scanning, and this will be called from multiple different places. I wanted to pass the Activity calling the scanner as a parameter, then when the scan is complete, pass the result back by running a function in the previous activity to process the result. This caused a crash, so I've created a little sample which is attached, but the relevant bits of code are below.

Calling activity:
Sub Button1_Click
    CallSubDelayed2(act1, "start", Activity)
End Sub

public Sub processResult(result As String)
    MsgboxAsync("Result is " & result, "Info")
End Sub

Returning activity:
Sub Globals
    Private parentActivity As Activity
End Sub

Sub Button1_Click
    CallSubDelayed2(parentActivity, "processResult", Activity.Title)
End Sub

Sub start(act As Activity)
    parentActivity = act
    MsgboxAsync("Called from " & parentActivity.Title,"Info")
End Sub

Public Sub processResult(result As String)
    MsgboxAsync("Result is " & result, "Info")
End Sub


I tap the button in the calling activity, and the returning activity opens and displays the title of the calling activity correctly.
When I tap the button to return the result to the calling activity, I get this error :

Error:
java.lang.RuntimeException: java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to java.lang.String
    at anywheresoftware.b4a.keywords.Common$12.run(Common.java:1215)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1226)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7941)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:569)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1019)
Caused by: java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to java.lang.String
    at anywheresoftware.b4a.keywords.Common.getComponentBA(Common.java:1252)
    at anywheresoftware.b4a.keywords.Common$12.run(Common.java:1158)
    ... 9 more

Any ideas why this is crashing with this error?

(I know I can achieve what I want in other ways so please don't suggest a different way. I want to understand why this is causing an error.)
 

Attachments

  • ActivityParameter.zip
    12.4 KB · Views: 41
Last edited:

drgottjr

Expert
Licensed User
Longtime User
now try it.
 

Attachments

  • ActivityParameter2.zip
    12.4 KB · Views: 38
Upvote 1
Top