Android Question anywheresoftware.b4a.objects.streams.File.OpenInput

D

Deleted member 103

Guest
Hi,

can someone tell me how to fix the error?

crash_01.PNG


In principle, everything is correct, I myself can not reproduce the error, but somehow, there are users with the errors are displayed.

The bitmap is present, since there are no doubts.
The variable is set in Main-Activity.

Main-Activity
B4X:
Sub Activity_Resume
    If Starter.strOldBackground<>mBBL.getBackgroundImageName(Starter.manager.GetString("imgBackground")) Then
        Starter.strOldBackground=mBBL.getBackgroundImageName(Starter.manager.GetString("imgBackground"))
    End If
   
    'Hindergrundbild laden
    Activity.SetBackgroundImage(LoadBitmapSample(File.DirAssets, Starter.strOldBackground & ".jpg",Activity.Width,Activity.Height))
End Sub

Activity-m4karten
B4X:
Sub Activity_Resume
    'Hindergrundbild laden
    Activity.SetBackgroundImage(LoadBitmapSample(File.DirAssets, Starter.strOldBackground & ".jpg",Activity.Width,Activity.Height))
   
End Sub

I do not understand what should be wrong in my code.:(
 

Star-Dust

Expert
Licensed User
Longtime User
Is it possible that Starter.strOldBackground does not return the correct name?
Can you first make a check with File.Exits to handle the error?

with Application_Error you could send the name of the missing file (Starter.strOldBackground) which would be null
 
Upvote 0
D

Deleted member 103

Guest
if the file would not exist, then the crash already in the activity Main and not later in the activity m4karten, right?
 
Upvote 0
D

Deleted member 103

Guest
In my case there is no need to try out whether the image exists or not, because the images are only in DirAssets, so the images need to be mandatory.

I just need to check in Main, using the sub "getBackgroundImageName", which bitmap is the current one.
solitario_02.png


Main-Activity
B4X:
Sub Activity_Resume
    If Starter.strOldBackground<>mBBL.getBackgroundImageName(Starter.manager.GetString("imgBackground")) Then
        Starter.strOldBackground=mBBL.getBackgroundImageName(Starter.manager.GetString("imgBackground"))
    End If
  
    'Hindergrundbild laden
    Activity.SetBackgroundImage(LoadBitmapSample(File.DirAssets, Starter.strOldBackground & ".jpg",Activity.Width,Activity.Height))
End Sub

and that is my sub.
B4X:
Sub getBackgroundImageName(strImage As String) As String
    Dim lstImage As List
    Dim index As Int
    lstImage.Initialize
    lstImage.AddAll(Array As String("tisch_01","tisch_02","tisch_03","tisch_04","tisch_05","tisch_06","tisch_07","tisch_08"))
   
    If strImage <> "" Then
        index = lstImage.IndexOf(strImage)
    Else
        index = 1
    End If
   
    If index > -1 Then
        Starter.manager.SetString("imgBackground", lstImage.Get(index))
        Return lstImage.Get(index)
    Else
        Starter.manager.SetString("imgBackground", lstImage.Get(1))
        Return lstImage.Get(1) '1=tisch_02
    End If
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In my case there is no need to try out whether the image exists or not,

Here:
wrong, because in the Main you don't check if the file exists, you check names.
I don't meant that you need to check if fhe image file exists; I commented this your statement:
if the file would not exist, then the crash already in the activity Main and not later in the activity m4karten, right?
which is wrong.
 
Upvote 0
D

Deleted member 103

Guest
I can only suggest to try your app on a device (or emulator) with the same user's O.S.
My Nexus-7 (2012) has exactly the same features as the device with the crash above.

I have however also tested with these devices:
Samsung-S4, Android 5.0.1
HTC-one-A9, Android 7.0
HTC-one-s, Android 4.1.1
HTC-Desire, Android 2.3
Medion LifeTab E7312, Android 4.2.2
Medion LifeTab X10302, Android 7.0
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Is there any more of the log? Somewhere it may tell you the name of the file that it cannot find. It would be useful to verify that it is trying to load the correct file.
 
Upvote 0
D

Deleted member 103

Guest
Is there any more of the log?
No, it always the same log file.

Somewhere it may tell you the name of the file that it cannot find.
Unfortunately the crash does not say which file is not found.

You can, if you like, try the app itself, here is the link: Solitario
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
One possibility is that the app is a little slow to grab the image, that will generate the File Not Found exception.

I haven't downloaded your app but by the looks of it it seems you support portrait and landscape, I'll bet that if the user flips the phone quickly the error occurs.
 
Upvote 0
D

Deleted member 103

Guest
I haven't downloaded your app but by the looks of it it seems you support portrait and landscape, I'll bet that if the user flips the phone quickly the error occurs.
I can understand it perhaps with this device,
solitario_03.png



but not with this.

solitario_04.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
if the file would not exist, then the crash already in the activity Main and not later in the activity m4karten, right?
No.

The mistake here is simple. You are not using the starter service correctly. As a general rule all global public variables should be declared an initialized in Service_Create of the starter service.

Your assumption that the program will always start from the main activity is wrong. If the user clicked on the home button when the second activity was in the foreground and the OS kills the process then later it will start from the second activity directly.
 
Upvote 0
D

Deleted member 103

Guest
It's funny what the users can do.
I tried to reproduce the same scenario, unfortunately without success.
1) I press the home button at the second activity
2) terminate the starter-service via the system-setting
3) and start the app
The app starts with the second activity without problems.
Well, maybe my devices are less sensitive than the other, or they know what to expect.
In any case, I need to make the initialization of variables in starter-service and not where else.
Thank you Erel.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Can it be a problem related to the device?

In my previous App I crack only on a specific device model, because for some reason I never knew I was deleting continuances and files I saved in the Temporary folder.

When I went to resume it I was mistaken. I had to make a check before each file reading to figure out that they had not been deleted.

I then removed that device from those on the list as fit to receive my App.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
No. Assuming that the main activity will start before other activities is a mistake. This is exactly why the starter service feature was added.
Yes, I read this explanation in a post that explained how the Starter module works.
 
Upvote 0
Top