W wl Well-Known Member Licensed User Longtime User Mar 26, 2012 #1 Hello, In a library I wrote I create a List of Map objects and pass it to an event The List itself is initialized as well as all Maps in this list. In the B4A event I have something like: Code: Copy Sub pop_DownloadAllMessagesCompleted (Success As Boolean, Size As Int, Count As Int, Messages As List, headers As List) For lcv = 0 To Count -1 Dim hdr As Map hdr = headers.Get(lcv) ListView1.AddSingleLine(hdr.Get("SUBJECT")) Next End Sub So I cast each item in the headers list as a Map. In runtime on this line I get a class cast exception. As a note: when I create a List of Maps in B4A all is OK. Any idea ? Thanks Last edited: Mar 26, 2012
Hello, In a library I wrote I create a List of Map objects and pass it to an event The List itself is initialized as well as all Maps in this list. In the B4A event I have something like: Code: Copy Sub pop_DownloadAllMessagesCompleted (Success As Boolean, Size As Int, Count As Int, Messages As List, headers As List) For lcv = 0 To Count -1 Dim hdr As Map hdr = headers.Get(lcv) ListView1.AddSingleLine(hdr.Get("SUBJECT")) Next End Sub So I cast each item in the headers list as a Map. In runtime on this line I get a class cast exception. As a note: when I create a List of Maps in B4A all is OK. Any idea ? Thanks
warwound Expert Licensed User Longtime User Mar 27, 2012 #2 In your library code are you creating and returning a anywheresoftware.b4a.objects.collections.List of anywheresoftware.b4a.objects.collections.Map or a java.util.List of java.util.Map? I've found that returning a native Java object to B4A sometimes works but B4A may choke on some native Java objects so you should create and return the B4A objects instead. Martin. Upvote 0
In your library code are you creating and returning a anywheresoftware.b4a.objects.collections.List of anywheresoftware.b4a.objects.collections.Map or a java.util.List of java.util.Map? I've found that returning a native Java object to B4A sometimes works but B4A may choke on some native Java objects so you should create and return the B4A objects instead. Martin.
W wl Well-Known Member Licensed User Longtime User Mar 27, 2012 #3 Hi, I was under the impression I was returning the Anywhere ... Map, this namespace was included, but I should check it again ... Upvote 0
Hi, I was under the impression I was returning the Anywhere ... Map, this namespace was included, but I should check it again ...
warwound Expert Licensed User Longtime User Mar 27, 2012 #4 You have to be explicit throughout. The compiler may cast a native Android object to a B4A wrapper but it's not the same. Look at this method from a library of mine, see how i've used fully qualified namespace to create and return a B4A List: Code: Copy public anywheresoftware.b4a.objects.collections.List GetTileSources() { anywheresoftware.b4a.objects.collections.List tileSources = new anywheresoftware.b4a.objects.collections.List(); tileSources.Initialize(); for (final ITileSource tileSource : TileSourceFactory.getTileSources()) { tileSources.Add(tileSource.name()); } return tileSources; } And since you are dealing with a B4A List and not a java.util.List, you have to use B4A methods to Initialize and Add objects to the B4A List. Martin. Upvote 0
You have to be explicit throughout. The compiler may cast a native Android object to a B4A wrapper but it's not the same. Look at this method from a library of mine, see how i've used fully qualified namespace to create and return a B4A List: Code: Copy public anywheresoftware.b4a.objects.collections.List GetTileSources() { anywheresoftware.b4a.objects.collections.List tileSources = new anywheresoftware.b4a.objects.collections.List(); tileSources.Initialize(); for (final ITileSource tileSource : TileSourceFactory.getTileSources()) { tileSources.Add(tileSource.name()); } return tileSources; } And since you are dealing with a B4A List and not a java.util.List, you have to use B4A methods to Initialize and Add objects to the B4A List. Martin.