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
StartActivity only schedules the activity to be started. It only actually starts when your application returns to scan the message loop so there is no errror to catch in your Try block.
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?
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.
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'?