Android Question crash on LoadLayout when installing over older version

Dave O

Well-Known Member
Licensed User
Longtime User
Hi all,

Unexplained "LoadLayout" crash only when I try to install a new test version of my app over top of an older version. I've never seen this before, despite a long history of version updates. Crashes in Release mode and Debug mode. Here's the Debug log:

B4X:
Copying updated assets files (40)
*** Service (starter) Create ***
[...some normal log statements]
** Service (starter) Start **
** Activity (main) Create (first time) **
java.lang.NoSuchFieldException: No field mFastScroll in class Landroid/widget/AbsListView; (declaration of 'android.widget.AbsListView' appears in /system/framework/framework.jar!classes4.dex)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.DynamicBuilder.build(DynamicBuilder.java:38)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:338)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:442)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:442)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:133)
    at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
    at name.obrien.dave.lister.main._createnavdrawer(main.java:1123)
    at name.obrien.dave.lister.main._activity_create(main.java:1045)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at name.obrien.dave.lister.main.afterFirstLayout(main.java:106)
    at name.obrien.dave.lister.main.access$000(main.java:18)
    at name.obrien.dave.lister.main$WaitForLayout.run(main.java:84)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:230)
    at android.os.Looper.loop(Looper.java:319)
    at android.app.ActivityThread.main(ActivityThread.java:8919)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at anywheresoftware.b4a.objects.ViewWrapper.buildNativeView(ViewWrapper.java:572)
    at anywheresoftware.b4a.objects.ButtonWrapper.build(ButtonWrapper.java:95)
    ... 26 more
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}, theme={InheritanceMap=[id=0x7f0c0005name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, id=0x7f0c0004name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info, id=0x1030200android:style/TextAppearance.Material.Notification.Info, id=0x10301feandroid:style/TextAppearance.Material.Notification, id=0x10301edandroid:style/TextAppearance.Material, id=0x103003eandroid:style/TextAppearance], Themes=[name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, forced, name.obrien.dave.lister:style/MyDarkTheme, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
    at android.content.res.TypedArray.getColor(TypedArray.java:529)
    at android.widget.TextView.readTextAppearance(TextView.java:4544)
    at android.widget.TextView.<init>(TextView.java:1442)
    at android.widget.Button.<init>(Button.java:174)
    at android.widget.Button.<init>(Button.java:149)
    at android.widget.Button.<init>(Button.java:125)
    at android.widget.Button.<init>(Button.java:104)
    ... 30 more
Error occurred on line: 470 (Main)
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Button).
Did you forget to call Activity.LoadLayout?
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:155)
    at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
    at name.obrien.dave.lister.main._createnavdrawer(main.java:1123)
    at name.obrien.dave.lister.main._activity_create(main.java:1045)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at name.obrien.dave.lister.main.afterFirstLayout(main.java:106)
    at name.obrien.dave.lister.main.access$000(main.java:18)
    at name.obrien.dave.lister.main$WaitForLayout.run(main.java:84)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:230)
    at android.os.Looper.loop(Looper.java:319)
    at android.app.ActivityThread.main(ActivityThread.java:8919)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: java.lang.RuntimeException: Object should first be initialized (Button).
Did you forget to call Activity.LoadLayout?
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:73)
    at anywheresoftware.b4a.objects.ButtonWrapper.innerInitialize(ButtonWrapper.java:39)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:423)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:442)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:442)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:133)
    ... 20 more
** Activity (main) Resume **

(The mFastScroll exception only happens in Debug mode and has been there for years, doesn't seem to affect anything.)

The LoadLayout is done when I create the nav drawer using AHNavigationDrawer. This has worked fine in the past and I haven't changed it since.

If I uninstall the previous version, this new version installs and runs just fine. That should be a clue, but I'm not sure where to go from there.

Another clue: This started after I got a new laptop. I installed B4A 13.4 and all referenced libraries using the latest instructions. Initially forgot to set my private keystore, but fixed that later.

I've tried installing on several different devices and Android versions, building with SDK 34, 35, and 36, with targetSDK set to 34 and 35 - same result.

Because the crash mentions theme stuff, I replaced my custom theme with a minimal standard one and it still crashes, same error. Has been a real head-scratcher so far.

Any ideas? Thanks!
 
Last edited:

josejad

Expert
Licensed User
Longtime User
Can you post your code around Loadlayout? Are you trying to do something with a button before loading the layout?
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
The layout is being loaded by the navigation drawer. This code and the layout have worked well for years, and still work if I do a fresh install (not over an older version), so not sure why I'm getting an error on it now:

B4X:
Sub createNavDrawer
    navDrawer.Initialize("navDrawer", Activity, Min(Activity.Width - c.ICON_SIZE1, 450dip))
    navDrawer.FitsSystemWindows = True

    Dim bmd As BitmapDrawable
    bmd.Initialize(c.generateBitmapFromIconCode(c.ICON_MENU, Colors.gray, 24, 48dip, False))
    slide.Initialize(bmd)
      
    drawerULV.Initialize(0, 0, "", "drawerULV")
    drawerULV.DividerHeight = 0
    drawerULV.SelectionMode = drawerULV.SELECTION_SINGLE
    drawerULV.AtLeastOneSelection = True
    drawerULV.SelectWithClick = True
    navDrawer.NavigationPanel.AddView(drawerULV, 0, 0, navDrawer.NavigationPanel.Width, navDrawer.NavigationPanel.Height)

    navDrawer.ContentPanel.LoadLayout("main")
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This line might suggest that it is related to the manifest editor:
Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}, theme={InheritanceMap=[id=0x7f0c0005name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, id=0x7f0c0004name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info, id=0x1030200android:style/TextAppearance.Material.Notification.Info, id=0x10301feandroid:style/TextAppearance.Material.Notification, id=0x10301edandroid:style/TextAppearance.Material, id=0x103003eandroid:style/TextAppearance], Themes=[name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, forced, name.obrien.dave.lister:style/MyDarkTheme, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.content.res.TypedArray.getColor(TypedArray.java:529)

Does the layout work properly with a new empty project?
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
This line might suggest that it is related to the manifest editor:
Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}, theme={InheritanceMap=[id=0x7f0c0005name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, id=0x7f0c0004name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info, id=0x1030200android:style/TextAppearance.Material.Notification.Info, id=0x10301feandroid:style/TextAppearance.Material.Notification, id=0x10301edandroid:style/TextAppearance.Material, id=0x103003eandroid:style/TextAppearance], Themes=[name.obrien.dave.lister:style/TextAppearance.Compat.Notification.Info.Media, forced, name.obrien.dave.lister:style/MyDarkTheme, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.content.res.TypedArray.getColor(TypedArray.java:529)
I don't use notifications or AppCompat anywhere in my code, so I don't know where this came from.

Does the layout work properly with a new empty project?
New empty project - It fails with a different error (resource not found) for the floatingActionButton component. When I deleted and re-added those 3 instances, it ran fine.

The strangest thing (to me) is that the original project works fine when installing fresh (no previous app version on the device). It only crashes when I install over top of another version, which maybe suggests some sort of caching problem?
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
I also tried converting the BAL to JSON using the B4J converter app, so I could see if there was anything suspicious in the JSON file. Nothing I could see.

Then I tried converting the JSON back to a BAL file, just in case that would "clean" it somehow. Same crash with that reconstituted BAL file too.

Argh.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
I removed the floatingActionButton from the layout, and commented out all mentions of it. Same crash, so the mystery remains...
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Just in case the BAL file was somehow corrupted, I recreated it from scratch with the same controls. Same result.

The strangest part is that it works just fine on a fresh install, but crashes when loaded over a previous version.

Copilots suggests the following. Thoughts on this?

Why It’s Crashing on “Install Over”​

When you install your B4A APK over an existing version, Android keeps the old application data and resources in memory. In your case:

  • Your custom theme is based on android:style/Theme.DeviceDefault.Light.DarkActionBar but you’re pulling in AppCompat‐style attributes (e.g. TextAppearance.Compat.Notification.Info.Media).
  • On an upgrade, the old in‐memory BA (Basic…) object and resources collide with the newly compiled layouts and styles.
  • When DynamicBuilder tries to instantiate a Button, the Android theme resolver looks for an AppCompat attribute that does not exist in your parent theme. That throws an UnsupportedOperationException, which leaves the B4A wrapper uninitialized—hence the “Object should first be initialized (Button)” error.

Fix 1: Switch to a True AppCompat Theme​

  1. Add the AppCompat Library
    • In the B4A IDE’s Libraries pane, check AppCompat (and AndroidX if your project uses it).
  2. Create a styles.xml under Additional Resources:
    B4X:
    [*]
    
    [*]<?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="MyDarkTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- customize your colors if desired -->
        <item name="colorPrimary">#3F51B5</item>
        <item name="colorPrimaryDark">#303F9F</item>
        <item name="colorAccent">#FF4081</item>
      </style>
    </resources>
  3. Apply MyDarkTheme in Project Attributes
    • Open Project → Attributes
    • Under Manifest set

    • B4X:
      android:theme="@style/MyDarkTheme"
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
The first time I install with a new package name, it runs fine (like it does with the old package name when not upgrading from an older version). So the layout and themes and such seem to be fine.

I tried bumping up the version code and installing over itself, and that worked fine too.

I then tried restoring the original package name and bumping the version code (hoping that might get rid of any old-version resources hanging around), but it crashed as before.

I need to keep the same package name so the new version behaves as the same app with the same data files intact.

The only obvious thing different from previous updates is that I'm on a new Windows 11 laptop (with a new installation of the same JDK 19 and the latest B4A) rather than my old Windows 10 laptop (wiped and sold to a friend).
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Here's a stripped-down version of the project to try.

The only third-party libraries left in are UltimateListView and AHNavigationDrawer, because they're entangled in the problem and ULV in particular is a huge part of the app. I've removed other modules and layouts.

Too large (788KB is too large?) to upload here, so I've uploaded it my public folder on my Google Drive:

To reproduce the crash:

  1. Compile and run this new version (v5.x) first. It should launch, but most functions won't work. That shows that the layout works on a fresh device.
  2. Uninstall it.
  3. Install Check Off from the Play Store (version 4.9.x). Run it once (so that it creates some sample checklists).
  4. Compile and run this new version over top of the old one. It will probably (hopefully?) crash on launch with the errors shown above.
If you need this stripped further (i.e. removing ULV and/or AHNavigationDrawer), just let me know and I'll give it a try.

Thanks all!
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Sorry about that. Here's a new zip that:
- deletes the first entry (that was for the FloatingActionButton library that I removed)
- adds the /icon folder (adaptive icon)

 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
I'll try removing the nav drawer. FYI, the app doesn't use MaterialMenuDrawable, so it doesn't need NineOldAndroids.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Removed the drawer and its library. Same crash when installing over an older version.

Updated project zip:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I ran it and tried to follow the steps you described, however as the new app is signed with my key, which is different than the key used to sign 4.9, I need to first uninstall the old version and can't really test it.

Things to check:
1. Unfiltered logs. Maybe more information there.
2. Are you using AppCompat views? If so then it is recommended to remove them. They are not needed and might be causing this issue.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
I went through the unfiltered logs but couldn't spot anything that helped explain the crash.

Not using the AppCompat library, and have avoided adding it to any of my apps because it always seemed more trouble than it was worth.

I'll try removing the last third-party component (UltimateListView) just to see if that helps, but my app is built around it so that would be both a win and a bummer.

Thanks for your time on this, much appreciated.
 
Upvote 0
Top