Android Question Null object error - only in Android 9

asales

Expert
Licensed User
Longtime User
I get this error in Firebase crashlitycs and show the devices system in all exceptions = Android 9:
B4X:
Fatal Exception: java.lang.NullPointerException
Attempt to read from field 'anywheresoftware.b4a.BA br.com.myprofiles.main.activityBA' on a null object reference
br.com.myprofiles.main$ResumableSub_Activity_Create.resume (main.java:1174)
br.com.myprofiles.main._activity_create (main.java:931)
Several of my apps has th
e same issue and I don't know why and how to fix it.

The lines of main.java (\Objects\src...) and main.bas are:
B4X:
main.java
929 - public static void  _activity_create(boolean _firsttime) throws Exception{
930 - ResumableSub_Activity_Create rsub = new ResumableSub_Activity_Create(null,_firsttime);
931 - rsub.resume(processBA, null);
932 - }

1167 - this.catchState = 0;
1168 -  //BA.debugLineNum = 276;BA.debugLine="If LastException.IsInitialized Then Log(LastExce";
1169 - if (true) break;
1170 -
1171 - case 32:
1172 - //if
1173 - this.state = 37;
1174 - if (anywheresoftware.b4a.keywords.Common.LastException(mostCurrent.activityBA).IsInitialized()) {
1175 - this.state = 34;
1176 - ;}if (true) break;

main.bas
try
(...)
275 - Catch
276 -     If LastException.IsInitialized Then Log(LastException.Message)  'check if LastException was initialized to avoid error
277 - End Try

Thanks in advance for any help.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Does this happen to all Android 9 users, or is it just a random, occasional crash? If the latter, it could be that the app was in the background & then shut down by the OS & not being restarted correctly. Are you using a Starter service?

- Colin.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
is it just a random, occasional crash?
Yes. I think is 1% of my users with Android 9.
Are you using a Starter service?
Yes, but why?
The lines in the error is a try..catch to load a splash screen with a little animation:
B4X:
Try
    Activity.LoadLayout("splash1")
    ivLogo.SetVisibleAnimated(800, True)
    AnimPlus.InitializeTranslate("Anim0", 0, 110dip, 0, 0)
    AnimPlus.SetInterpolator(AnimPlus.INTERPOLATOR_DECELERATE)
    AnimPlus.StartOffset = 0
    AnimPlus.Duration = 1400
    AnimPlus.PersistAfter = True
    AnimPlus.Start(ivProfile)
Catch
    If LastException.IsInitialized Then Log(LastException.Message)  '<- here is the error in the line 276 (log of firebase crashlytics)
End Try

Sleep(1000)

CreateMainScreen
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Can you post the complete Activity_Create code?
Here:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
    Log($"Date/time: $Date{DateTime.Now} $Time{DateTime.Now}"$)
            
    'counter to update native ad
    GlobalTimeSetWhenStopperWasStarted = DateTime.Now
    
    SetDefaults 'create preferences

    'show tip in the first open
    If Starter.manager.GetBoolean("tip001") = False Then
        StartActivity(abertura)   
        Starter.manager.SetBoolean("tip001", True)
        Sleep(0)   
    End If
        
    Try
        billman3.Initialize("billman3", chv)
        billman3.DebugLogging = False
    Catch
        Log(LastException)
        ToastMessageShow("Error to check In-App", False)
    End Try

    NavDrawer.Initialize2("NavDrawer", Activity, NavDrawer.DefaultDrawerWidth, NavDrawer.GRAVITY_START)

    actual_hour = DateTime.GetHour(DateTime.Now)
    actual_min = DateTime.GetMinute(DateTime.Now)
    
    'splash screen
    Try 
        Activity.LoadLayout("splash1")       
        ivLogo.SetVisibleAnimated(800, True)
        AnimPlus.InitializeTranslate("Anim0", 0, 110dip, 0, 0)
        AnimPlus.SetInterpolator(AnimPlus.INTERPOLATOR_DECELERATE)
        AnimPlus.StartOffset = 0
        AnimPlus.Duration = 1400
        AnimPlus.PersistAfter = True
        AnimPlus.Start(ivProfile)
    Catch
        If LastException.IsInitialized Then Log(LastException.Message) '<===== HERE IS THE LINE OF ERROR SHOW IN FIREBASE CRASHLITYCS
    End Try
    
    'record open date/hour + last access
    Dim fVal As Map
    fVal.Initialize 'inicializa mapa
    Try
        If Funcoes.ExistsValueDB("diary", "data", Funcoes.GetDefaultDateFormat(DateTime.Date(DateTime.Now))) = False Then
            Dim ListOfMaps As List
            ListOfMaps.Initialize
            fVal.Put("date", Funcoes.GetDefaultDateFormat(DateTime.Date(DateTime.Now)))
            fVal.Put("hour", DateTime.Time(DateTime.Now))
            ListOfMaps.Add(fVal)
            DBUtils.InsertMaps(Starter.SQL1, "diary", ListOfMaps)
        End If
    Catch
        Log(LastException.Message)
    End Try
        
    Counter = 0
        
    'initialize timers   
    Timer1.Initialize("Timer1", 70)

    TimerMsg.Initialize("TimerMsg", 400)
    
    Sleep(1000)
    
    CreateMainScreen  'call sub to create the main screen
End Sub

Sub Activity_Resume
    Starter.running = True
    updated = False
    
    '*** notification
    Dim in As Intent
    Try
        in = Activity.GetStartingIntent
        
        If in.IsInitialized And in.HasExtra("Notification_Tag") Then
            notifica = True
        
            Select Starter.momento
                Case 2  'night
                    If Funcoes.CheckConnection = True Then Starter.analytics.SendEvent("Notifica_Night", Null)
                Case 1  'evening
                    If Funcoes.CheckConnection = True Then Starter.analytics.SendEvent("Notifica_Evening", Null)
                Case Else
                    If Funcoes.CheckConnection = True Then Starter.analytics.SendEvent("Notifica_Morning", Null)
            End Select
        
        End If
    Catch
        Log(LastException)
    End Try
    
    If Starter.free = False Then 'only show ad in the free version
        If Starter.ShowAd = True Then ShowAd

        'update native ads
        CheckAds
    End If
    
    If TTS1.IsInitialized = False Then
        TTS1.Initialize("TTS1")
        TTS1.SpeechRate = 0.9
    End If
    
    If AdView1.IsInitialized Then AdView1.Resume
End Sub

Sub CreateMainScreen       
    pSplash.Visible = False 
    
    'check screen sizes   
    If GetDeviceLayoutValues.ApproximateScreenSize > 6.5 Then
        'tablets
        iheight = 92dip
        IsTablet = True
    Else
        'phones
        iheight = 52dip
        IsTablet = False
    End If   
    
    'load main layout
    Activity.LoadLayout("main0")
    
    'load main layout 2 inside scrollview
    scv1.Panel.LoadLayout("main1")
    
    'create actionbar
    AB.Initialize(pnlAB, True, False, pnlAB.height, False, Me) 
    Create_ActionBar
    
    CreateMenu
    
 (....)
End sub
 
Upvote 0

asales

Expert
Licensed User
Longtime User
The problem is probably here:
B4X:
  StartActivity(abertura)  
        Starter.manager.SetBoolean("tip001", True)
        Sleep(0)
As you are calling Sleep(0), you are allowing the next activity to start and the current activity to be paused. Add a Return after Sleep(0).
I don't understand?
This activity ("abertura") is a Welcome activity. I show it once time, before the splash screen, when the app starts on first time and don't show it again.
If I put a "return" after the sleep(0), the Welcome screen is show, but the Splash and Main activities is not load.
 
Upvote 0
Top