Italian Rotazione schermo - App crasha

Sabotto

Active Member
Licensed User
Ho il problema classico che ruotando lo schermo l'app crasha e ho questo errore in debug (e non riesco a capire il problema perchè non è indicata una linea di codice) :
B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{b4a.VediTurno/b4a.VediTurno.main}: android.content.res.Resources$NotFoundException: Drawable b4a.VediTurno:drawable/icon with resource ID #0x7f020000
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5930)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5820)
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Caused by: android.content.res.Resources$NotFoundException: Drawable b4a.VediTurno:drawable/icon with resource ID #0x7f020000
Caused by: java.lang.IllegalArgumentException: cannot use a recycled source in createBitmap
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1029)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:954)
    at huawei.android.hwutil.IconBitmapUtils.zoomIfNeed(IconBitmapUtils.java:173)
    at android.content.res.HwResourcesImpl.handleAddIconBackground(HwResourcesImpl.java:504)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:930)
    at android.content.res.Resources.getDrawableForDensity(Resources.java:994)
    at android.content.res.Resources.getDrawable(Resources.java:933)
    at android.content.Context.getDrawable(Context.java:687)
    at com.android.internal.widget.ToolbarWidgetWrapper.setIcon(ToolbarWidgetWrapper.java:333)
    at com.android.internal.widget.ActionBarOverlayLayout.setIcon(ActionBarOverlayLayout.java:921)
    at com.android.internal.policy.PhoneWindow.setDefaultIcon(PhoneWindow.java:1860)
    at android.app.Activity.initWindowDecorActionBar(Activity.java:3445)
    at android.app.Activity.setContentView(Activity.java:3480)
    at b4a.VediTurno.main.onCreate(main.java:62)
    at android.app.Activity.performCreate(Activity.java:8086)
    at android.app.Activity.performCreate(Activity.java:8074)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3755)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5930)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5820)
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
Ho letto dei post dove il problema della rotazione, provocando l'evento Create, da problemi per le variabili globals che si azzerano
Ma io nella Globals ho solo le view (label e bottoni)
Ho però un po di roba nella Create (perche accedo al mio dropbox); forse il problema è li?

Ecco la parte interessata di codice
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim auth As DbxAuth
    Dim Dropbox As DropboxV2
    Dim token As String
    Dim client As DbxClientV2
    Dim config As DbxRequestConfig
    Dim dbxFiles As DbxUserFilesRequests
    Dim dbxSharing As DbxUserSharingRequests
    Dim dbxUsers As DbxUserUsersRequests
    Dim RP As RuntimePermissions
   
    Dim DataOggi As Long = DateTime.Now
    Dim Anno As Int = DateTime.GetYear(DataOggi)
    Dim NomeMese As String
    Dim Giorno As Int = DateTime.GetDayOfMonth(DataOggi)
    Dim NumGiorniMese As Int
   
    Dim NomeFileTurno As String ' il file ".TEL"
    Dim NomeFileTurnoPDF As String ' il file ".PDF"
    Dim RigaTurno(6) As String ' da 0 a 5
    Dim TurnoDelGiorno(6) As String, TurnoDelGiornoSuccess(6) As String ' da 0 a 5
    Dim DirDestination As String
    Dim DirTurni_DropBox As String = "/DropBox Lavoro/Turni Telecontrollo"
    Dim NomeDip(7) As String
           
End Sub

Sub Globals
   
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Private btnStartOAuth As Button
    Private lblMattina As Label
    Private lblNotte As Label
    Private lblPomeriggio As Label
    Private lblNotteSuccessiva As Label
    Private lblAssente As Label
    Private lblDataTurno As Label
    Private btnGGAvanti As Button
    Private btnGGDietro As Button
    Private btnVediPDF As Button
    Private btnEsci As Button
    Private lblIniziale As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
       
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("layMainTurno")
       
    Dim dummy As String
    dummy = Starter.kvs.GetDefault("token", "")
    If dummy <> "" Then
        token = dummy
        Log("  - - - - - - Sono nel Create nel dummy: Il Token è già disponibile - - - - ")
        config.Initialize("",token,"","de-de",5)
        Dim dbxhost As DbxHost
        dbxhost.Initialize
        client.Initialize("Dropbox",config,token,dbxhost)
        dbxFiles = client.files
        dbxFiles.setEventname("dbxFiles")
        dbxSharing = client.sharing
        dbxSharing.setEventname("dbxSharing")
        dbxUsers = client.users
        dbxUsers.setEventname("dbxUsers")  
    Else
        Log("  - - - - - - Sono nel Create bypassato il dummy che era vuoto- - - - ")
    End If
   
    Dropbox.Initialize("")
    auth.Initialize("miaKey_xxxxxxxxxxxxxxxx")
   
    SchermataIniziale
   
End Sub

Sub Activity_Resume
    If auth.OAuth2Token <> Null Then
        If auth.OAuth2Token <> "" Then
            token = auth.OAuth2Token
            'btnListroot.Enabled = True
            Starter.kvs.Put("token", token)
            Log(" - - -Siamo nel Resume. Il Token è disponibile e quindi Dropbox è abilitato - - -")
            config.Initialize("",token,"","de-de",5)
            Dim dbxhost As DbxHost
            dbxhost.Initialize
            client.Initialize("Dropbox",config,token,dbxhost)
            dbxFiles = client.files
            dbxFiles.setEventname("dbxFiles")
            dbxSharing = client.sharing
            dbxSharing.setEventname("dbxSharing")
            dbxUsers = client.users
            dbxUsers.setEventname("dbxUsers")
        Else
            Log($"Token = """$)
        End If
    Else
        Log("Il Token 'Oauth2Token' nel resume è NULL")
        If token <> "" Then
            Log("Provo ad usare il token conosciuto...")
            dbxUsers.CurrentAccount ' Tested!
        Else
            Log ("Non c'è nessun token. Occorre chiamare la routine")
            MsgboxAsync ("Non c'è nessun token. Occorre chiamare la routine","TOKEN ASSENTE")  
        End If
   
    End If
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub SchermataIniziale

    DirDestination = RP.GetSafeDirDefaultExternal("TurniTel")
    RicavaNomeMese
    LeggeNomiDip
       
    Wait For (DownloadTurno) Complete (Result As Boolean)
    If Not(Result) Then
        Msgbox("Errore. Chiudo la app","") 'ignore
        'ed esco
        ExitApplication
    End If
    MostraBottoni
    RicavaRigheTurni
    Giorno=DateTime.GetDayOfMonth(DataOggi)
    GetTurnoGiornoAndShow
   
End Sub
Cosa dovrei cambiare?
Oppure: non è possibile far si che la rotazione dello schermo non venga proprio presa in considerazione? (penso di no, peò, giusto?)
 
Last edited:

Sagenut

Expert
Licensed User
Longtime User
In Project Attributes (all'inizio della tua Activity) puoi fissare l'orientamento desiderato:
B4X:
'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
In questo esempio l'app resterà sempre in Portrait.
Le B4XPages hanno il PROBLEMA (in questo caso per te sarebbe un aiuto 😁 ) di non supportare la rotazione al momento.
 

LucaMs

Expert
Licensed User
Longtime User
Se fissasse l'orientamento dell'app non avrebbe problemi nemmeno con le B4XPages ma suppongo che voglia consentire all'utente di poter ruotare il dispositivo e vedere un layout adatto.

Ho letto dei post dove il problema della rotazione, provocando l'evento Create, da problemi per le variabili globals che si azzerano
Nelle Activity hai DUE routine Globals, la Process_Globals e la Globals. Non si deve fare confusione tra queste due.

Ciò che si trova nella Process_Globals (quindi a livello di processo e non locale all'Activity) non sarà "intaccato" dalla rotazione.
Quindi una variabile dichiarata nella Process_Globals non verrà azzerata quando venisse ruotato il dispositivo. Le variabili dichiarate invece nella Globals (variabili che quindi esistono solo nell'Activity, non in tutto il progetto) verranno riportare al valore di default.

Quando si ruota il dispositivo viene effettivamente eseguita nuovamente la routine-evento Activity_Create ma in questo caso la variabile isFirst avrà valore False.
Alcune inizializzazioni che fai nella Activity_Create, quindi, dovresti metterle all'interno di un:
B4X:
If First = True Then
' inizializzazioni
End If
altrimenti queste verrebbero eseguite più volte creando problemi.
 
Last edited:

Sabotto

Active Member
Licensed User
Grazie ad entrambe. Si mi va bene che l'app sia solo portrait, quindi userò
#SupportedOrientations: portrait
Ma, per mia scienza, riuscite a capire perchè il nuovo "Create" mi fa crashare?
Dovrebbe essere come se rilanciassi l'app.
 
Top