Java Question java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.List

sonicmayne

Member
Licensed User
I am writing a method that will return a list, with each item being a map containing information about an app installed on the device.

I have the method working to the extent that I can get the list, and access each map, however some of the items in the map are lists, which I cannot access. I have verified that I am getting a B4A list by writing another method as shown below.

B4X:
    public boolean getIsB4AList(Object List) {
        return List != null && List.getClass().equals(anywheresoftware.b4a.objects.collections.List.class);
    }
Which when called from B4A, returns true.

However when I try dimming a list, it throws
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.List

My Java method is here:

B4X:
    /**
     * Returns a list, with each element being a map containing extended details
     * about the relevant app.
     */
    public anywheresoftware.b4a.objects.collections.List getInstalledPackages() {
        anywheresoftware.b4a.objects.collections.List list = new anywheresoftware.b4a.objects.collections.List();
        list.Initialize();
        PackageManager pm = BA.context.getPackageManager();

        java.util.List<PackageInfo> temp = pm
                .getInstalledPackages(PackageManager.GET_ACTIVITIES
                        | PackageManager.GET_GIDS
                        | PackageManager.GET_CONFIGURATIONS
                        | PackageManager.GET_INSTRUMENTATION
                        | PackageManager.GET_INSTRUMENTATION
                        | PackageManager.GET_RECEIVERS
                        | PackageManager.GET_RECEIVERS
                        | PackageManager.GET_SERVICES
                        | PackageManager.GET_SIGNATURES);

        for (PackageInfo pi : temp) {
            MyMap map = new MyMap();
            ActivityInfo[] acts = pi.activities;
            if (acts != null) {
                anywheresoftware.b4a.objects.collections.List actlist = new anywheresoftware.b4a.objects.collections.List();
                actlist.Initialize();
                for (ActivityInfo a : acts) {
                    MyMap actmap = new MyMap();
                    if (IsAPIOrBetter(20)) {
                        actmap.put("Banner", a.banner);
                    }
                    actmap.put("ConfigChanges", a.configChanges);
                    if (IsAPIOrBetter(21)) {
                        actmap.put("DocumentLaunchMode", a.documentLaunchMode);
                    }
                    actmap.put("Enabled", a.enabled);
                    actmap.put("Exported", a.exported);
                    actmap.put("Flags", a.flags);
                    if (IsAPIOrBetter(9)) {
                        actmap.put("Logo", a.logo);
                    }
                    if (IsAPIOrBetter(21)) {
                        actmap.put("MaxRecents", a.maxRecents);
                    }
                    actmap.put("Name", a.name);
                    actmap.put("PackageName", a.packageName);
                    actmap.put("Permission", a.permission);
                    if (IsAPIOrBetter(21)) {
                        actmap.put("PersitableMode", a.persistableMode);
                    }
                    actmap.put("ProcessName", a.processName);
                    actmap.put("ScreenOrientation", a.screenOrientation);
                    if (IsAPIOrBetter(3)) {
                        actmap.put("SoftInputMode", a.softInputMode);
                    }
                    actmap.put("TargetActivity", a.targetActivity);
                    actmap.put("TaskAffinity", a.taskAffinity);
                    actmap.put("Theme", a.theme);
                    if (IsAPIOrBetter(14)) {
                        actmap.put("UIOptions", a.uiOptions);
                    }
                    actlist.Add(actmap);
                }
                map.put("Activities", actlist);
            }
            if (pi.applicationInfo != null) {
                ApplicationInfo ai = pi.applicationInfo;
                MyMap appmap = new MyMap();
                if (IsAPIOrBetter(8)) {
                    appmap.put("BackupAgentName", ai.backupAgentName);
                }
                appmap.put("ClassName", ai.className);
                if (IsAPIOrBetter(13)) {
                    appmap.put("CompatibleWidthLimitDP",
                            ai.compatibleWidthLimitDp);
                }
                appmap.put("DataDir", ai.dataDir);
                appmap.put("Enabled", ai.flags);
                appmap.put("Flags", ai.flags);
                try {
                    appmap.put("Label", BA.context.getString(ai.labelRes));
                } catch (Exception ex) {

                }
                if (IsAPIOrBetter(13)) {
                    appmap.put("LargestWidthLimitDP", ai.largestWidthLimitDp);
                }
                appmap.put("ManageSpaceActivityName",
                        ai.manageSpaceActivityName);
                appmap.put("Name", ai.name);
                if (IsAPIOrBetter(9)) {
                    appmap.put("NativeLibraryDir", ai.nativeLibraryDir);
                }
                appmap.put("NonLocalizedLabel", ai.nonLocalizedLabel);
                if (IsAPIOrBetter(13)) {
                    appmap.put("RequiresSmallestWidthDP",
                            ai.requiresSmallestWidthDp);
                }
                appmap.put("PackageName", ai.packageName);
                appmap.put("Permission", ai.permission);
                appmap.put("ProcessName", ai.processName);
                appmap.put("PublicSourceDir", ai.publicSourceDir);
                appmap.put("SharedLibraryFiles", ai.sharedLibraryFiles);
                appmap.put("SourceDir", ai.sourceDir);
                if (IsAPIOrBetter(21)) {
                    appmap.put("SplitPublicSourceDirs",
                            ai.splitPublicSourceDirs);
                    appmap.put("SplitSourceDirs", ai.splitSourceDirs);
                }
                if (IsAPIOrBetter(4)) {
                    appmap.put("TargetSDKVersion", ai.targetSdkVersion);
                }
                appmap.put("TaskAffinity", ai.taskAffinity);
                appmap.put("Theme", ai.theme);
                appmap.put("UID", ai.uid);
                if (IsAPIOrBetter(14)) {
                    appmap.put("UIOptions", ai.uiOptions);
                }
                map.put("ApplicationInfo", appmap);
            }
            if (IsAPIOrBetter(22)) {
                map.put("BaseRevisionCode", pi.baseRevisionCode);
            }
            if (IsAPIOrBetter(3) && pi.configPreferences != null) {
                ConfigurationInfo[] ci = pi.configPreferences;
                anywheresoftware.b4a.objects.collections.List configlist = new anywheresoftware.b4a.objects.collections.List();
                configlist.Initialize();
                for (ConfigurationInfo c : ci) {
                    MyMap cmap = new MyMap();
                    if (IsAPIOrBetter(4)) {
                        cmap.put("RequiredGLESVersion", c.reqGlEsVersion);
                    }
                    cmap.put("RequiredInputFeatures", c.reqInputFeatures);
                    cmap.put("RequiredKeyBoardType", c.reqKeyboardType);
                    cmap.put("RequiredNavigation", c.reqNavigation);
                    cmap.put("RequiredTouchScreen", c.reqTouchScreen);
                    configlist.Add(cmap);
                }
                map.put("ConfigPreferences", configlist);
            }
            if (IsAPIOrBetter(9)) {
                map.put("FirstInstallTime", pi.firstInstallTime);
            }
            map.put("GIDs", pi.gids);
            if (IsAPIOrBetter(21)) {
                map.put("InstallLocation", pi.installLocation);
            }
            if (pi.instrumentation != null) {
                InstrumentationInfo[] ii = pi.instrumentation;
                anywheresoftware.b4a.objects.collections.List instlist = new anywheresoftware.b4a.objects.collections.List();
                instlist.Initialize();
                for (InstrumentationInfo i : ii) {
                    MyMap imap = new MyMap();
                    imap.put("DataDir", i.dataDir);
                    imap.put("FunctionalTest", i.functionalTest);
                    imap.put("HandleProfiling", i.handleProfiling);
                    imap.put("PublicSourceDir", i.publicSourceDir);
                    imap.put("SourceDir", i.sourceDir);
                    if (IsAPIOrBetter(21)) {
                        imap.put("SplitPublicSourceDirs",
                                i.splitPublicSourceDirs);
                        imap.put("SplitSourceDirs", i.splitSourceDirs);
                    }
                    imap.put("TargetPackage", i.targetPackage);
                    instlist.Add(imap);
                }
                map.put("Instrumentation", instlist);
            }
            if (IsAPIOrBetter(9)) {
                map.put("LastUpdateTime", pi.lastUpdateTime);
            }
            map.put("PackageName", pi.packageName);
            if (pi.permissions != null) {
                PermissionInfo[] pi2 = pi.permissions;
                anywheresoftware.b4a.objects.collections.List plist = new anywheresoftware.b4a.objects.collections.List();
                plist.Initialize();
                for (PermissionInfo p : pi2) {
                    MyMap pmap = new MyMap();
                    if (IsAPIOrBetter(17)) {
                        pmap.put("Flags", p.flags);
                    }
                    pmap.put("Group", p.group);
                    pmap.put("ProtectionLevel", p.protectionLevel);
                    plist.Add(pmap);
                }
            }
            if (pi.providers != null) {
                ProviderInfo[] pi3 = pi.providers;
                anywheresoftware.b4a.objects.collections.List plist2 = new anywheresoftware.b4a.objects.collections.List();
                plist2.Initialize();
                for (ProviderInfo p : pi3) {
                    MyMap pmap = new MyMap();
                    pmap.put("Authority", p.authority);
                    if (IsAPIOrBetter(17)) {
                        pmap.put("Flags", p.flags);
                    }
                    pmap.put("GrantURIPermissions", p.grantUriPermissions);
                    pmap.put("InitOrder", p.initOrder);
                    pmap.put("MultiProcess", p.multiprocess);
                    pmap.put("ReadPermission", p.readPermission);
                    pmap.put("WritePermission", p.writePermission);
                    plist2.Add(pmap);
                }
                map.put("Providers", plist2);
            }
            if (pi.receivers != null) {
                ApplicationInfo ai = pi.applicationInfo;
                MyMap appmap = new MyMap();
                if (IsAPIOrBetter(8)) {
                    appmap.put("BackupAgentName", ai.backupAgentName);
                }
                appmap.put("ClassName", ai.className);
                if (IsAPIOrBetter(13)) {
                    appmap.put("CompatibleWidthLimitDP",
                            ai.compatibleWidthLimitDp);
                }
                appmap.put("DataDir", ai.dataDir);
                appmap.put("Enabled", ai.flags);
                appmap.put("Flags", ai.flags);
                try {
                    appmap.put("Label", BA.context.getString(ai.labelRes));
                } catch (Exception ex) {

                }
                if (IsAPIOrBetter(13)) {
                    appmap.put("LargestWidthLimitDP", ai.largestWidthLimitDp);
                }
                appmap.put("ManageSpaceActivityName",
                        ai.manageSpaceActivityName);
                appmap.put("Name", ai.name);
                if (IsAPIOrBetter(9)) {
                    appmap.put("NativeLibraryDir", ai.nativeLibraryDir);
                }
                appmap.put("NonLocalizedLabel", ai.nonLocalizedLabel);
                if (IsAPIOrBetter(13)) {
                    appmap.put("RequiresSmallestWidthDP",
                            ai.requiresSmallestWidthDp);
                }
                appmap.put("PackageName", ai.packageName);
                appmap.put("Permission", ai.permission);
                appmap.put("ProcessName", ai.processName);
                appmap.put("PublicSourceDir", ai.publicSourceDir);
                appmap.put("SharedLibraryFiles", ai.sharedLibraryFiles);
                appmap.put("SourceDir", ai.sourceDir);
                if (IsAPIOrBetter(21)) {
                    appmap.put("SplitPublicSourceDirs",
                            ai.splitPublicSourceDirs);
                    appmap.put("SplitSourceDirs", ai.splitSourceDirs);
                }
                if (IsAPIOrBetter(4)) {
                    appmap.put("TargetSDKVersion", ai.targetSdkVersion);
                }
                appmap.put("TaskAffinity", ai.taskAffinity);
                appmap.put("Theme", ai.theme);
                appmap.put("UID", ai.uid);
                if (IsAPIOrBetter(14)) {
                    appmap.put("UIOptions", ai.uiOptions);
                }
                map.put("Receivers", appmap);
            }
            if (IsAPIOrBetter(5) && pi.reqFeatures != null) {
                FeatureInfo[] fi = pi.reqFeatures;
                anywheresoftware.b4a.objects.collections.List flist = new anywheresoftware.b4a.objects.collections.List();
                flist.Initialize();
                for (FeatureInfo f : fi) {
                    MyMap fmap = new MyMap();
                    fmap.put("Flags", f.flags);
                    fmap.put("Name", f.name);
                    fmap.put("RequiredGLESVersion", f.reqGlEsVersion);
                    flist.Add(fmap);
                }
                map.put("RequiredFeatures", flist);
            }
            map.put("RequestedPermissions", pi.requestedPermissions);
            if (IsAPIOrBetter(16)) {
                map.put("RequestedPermissionsFlags",
                        pi.requestedPermissionsFlags);
            }
            if (pi.services != null) {
                ServiceInfo[] si = pi.services;
                anywheresoftware.b4a.objects.collections.List slist = new anywheresoftware.b4a.objects.collections.List();
                slist.Initialize();
                for (ServiceInfo s : si) {
                    MyMap smap = new MyMap();
                    if (IsAPIOrBetter(14)) {
                        smap.put("Flags", s.flags);
                    }
                    smap.put("Permission", s.permission);
                    smap.put("Name", s.name);
                    smap.put("ProcessName", s.processName);
                    slist.Add(smap);
                }
                map.put("Services", slist);
            }
            if (IsAPIOrBetter(3)) {
                map.put("SharedUserID", pi.sharedUserId);
                map.put("SharedUserLabel", pi.sharedUserLabel);
            }
            if (IsAPIOrBetter(21)) {
                map.put("SplitNames", pi.splitNames);
            }
            if (IsAPIOrBetter(22)) {
                map.put("SplitRevisionCodes", pi.splitRevisionCodes);
            }
            map.put("VersionCode", pi.versionCode);
            map.put("VersionName", pi.versionName);
            list.Add(map);
        }
        return list;
    }
And my code in B4A is here:
B4X:
    Dim pu As PackageUtils
    pu.Initialize
    Dim List As List = pu.InstalledPackages 'Get the installed packages.
    For i = 0 To List.Size - 1
        Dim Map As Map = List.Get(i) 'Get the map for this app.
        Dim o As Object = Map.Get("Services") 'Just, a test, get the services. This works and returns a valid list.
        Log(o) 'Outputs the items in the list.
        Log("B4A: " & pu.getIsB4AList(o)) 'Returns true
        Log("Java: " & pu.getIsJavaList(o)) 'Returns false.
        If pu.getIsJavaList(o) Then LogColor("Oh no!",Colors.Red) 'Not executed.
        If pu.getIsB4AList(o) Then
            Dim l As List = o 'Throws the exception mentioned.
            If l.IsInitialized Then
                Log(l.Size)
            End If
        End If
    Next
I've been scratching my head over this for hours now.

Any help is appreciated, thanks.
 

sonicmayne

Member
Licensed User
I had already tried Dim l as list = Map.Get("Services"), which threw the same error.

Below is the stack trace

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


null


B4A: false
Java: false
(ArrayList) [{Permission=null, Name=com.android.server.LoadAverageService, ProcessName=system}, {Permission=android.permission.BIND_WALLPAPER, Name=com.android.internal.service.wallpaper.ImageWallpaper, ProcessName=system}, {Permission=android.permission.MASTER_CLEAR, Name=com.android.internal.os.storage.ExternalStorageFormatter, ProcessName=system}, {Permission=null, Name=com.android.internal.os.storage.ExternalStorageFormatterSd, ProcessName=system}]
B4A: true
Java: false
main_activity_create (B4A line: 40)


Dim l As List = o 'Throws the exception mention
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.List
at com.mathew.packagetest.main._activity_create(main.java:388)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at com.mathew.packagetest.main.afterFirstLayout(main.java:100)
at com.mathew.packagetest.main.access$100(main.java:17)
at com.mathew.packagetest.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
 

sonicmayne

Member
Licensed User
That's why I was confused and wrote the method to test I was actually getting the correct object back.
 

sonicmayne

Member
Licensed User
I finally solved it. Instead of adding B4A lists to the maps, I'm adding ArrayLists, it works and I can get the data from the lists now.

Thanks for your help
 
Top