Try Catch StartActivity

tsdtb4a

Member
Licensed User
Longtime User
Hi

I try to run a activity module that does not exist and wish to catch the exception using a try catch, i.e. catch a LastException java.lang.ClassCastException: java.lang.NoClassDefFoundError as follows

But my program remains prompting an error and does not move to catch codes.

My codes as follow:
Sub StartTests(testname As String)

Try
StartActivity(test)
Catch
'WriteToDebugWnd("Test not availale: " & test & " " & LastException.Message)
End Try

End Sub

Did I miss out anything?

Thanks,
 

tsdtb4a

Member
Licensed User
Longtime User
Codes edit

My codes as follow:
Sub StartTests(test As String)

Try
StartActivity(test)
Catch
'WriteToDebugWnd("Test not availale: " & test & " " & LastException.Message)
End Try

End Sub
 
Upvote 0

tsdtb4a

Member
Licensed User
Longtime User
Thanks Agraham

Any way of determing whether the activity exists before I call StartActivity(activityname) so that I can log an error when the activity is not available and move on to the next activity without an error prompt?
 
Upvote 0

tsdtb4a

Member
Licensed User
Longtime User
The reason being my app using b4a consist of a framework that allows user to configure activities to be run based using a config file. All activities might be built into it but older activities may be also removed on newer versions as time goes by.

From the config file, the application parse the activities to call. If the activitiy is not available, it should "elegantly" log that this activity is skipped and runs the next activity in the config file.

I had use this framework idea on Basic4PPC for my app (i.e. calling .bas modules based on user config file) and wish to extend the same framework model to b4a for my user.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Try this using my Reflection library.
B4X:
Dim Obj1 As Reflector
Dim act, msg As String
act = "my.package.name.activityname" ' must be correct case
Try
   Obj1.Target = Obj1.GetContext ' Application
   Obj1.Target = Obj1.RunMethod("getClassLoader") ' ClassLoader
   Obj1.Target =  Obj1.RunMethod2("findClass", act, "java.lang.String")
   msg = "Present"
Catch
   msg = "Not present"
End Try
Msgbox(msg, act)
 
Upvote 0

rinoc62

New Member
Licensed User
Longtime User
Hi.
I have the same issue of tsdtb4a, but if I try your solution it doesn't work.

B4X:
Dim Obj1 As Reflector
Dim act As String
act = cdVG.strPackName & ".a" & strValue '  strPackName='rcs.LIBOR' and strValue = 'tabEsercizi'
Try
    Obj1.Target = Obj1.GetContext ' Application
    Obj1.Target = Obj1.RunMethod("getClassLoader") ' ClassLoader
    Obj1.Target =  Obj1.RunMethod2("findClass", act, "java.lang.String")
   StartActivity("a" & strValue)
Catch
   lvwSottoMenu.Visible = True
   Msgbox("Not present", act)         
End Try

I just dont understand what is wrong.
Could you help me?
Thank you very much.
Rinoc62
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Tried removing the startActivity from inside the try-catch block? You can follow Andrew's code, and start the activity outside of the block, if message equals to present.
B4X:
if msg="Present" then startactivity("a" & strValue)
By the way, what does the 'a' stands for? I mean, shouldn't it be simply the name of your activity or all activities' names in your project start with an 'a'?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…