Other [quiz] Find the bug

Discussion in 'Android Questions' started by Erel, Jul 13, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub Process_Globals
       
    Public Image As Bitmap 'public image
       Private SQL1 As SQL 'private sql object
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    If FirstTime Then
         
    Image = LoadBitmap(File.DirAssets, "Image.jpg")
         SQL1.Initialize(
    File.DirInternal, "1.db"True)
       
    End If
    End Sub
    Solution: http://www.b4x.com/android/forum/threads/quiz-find-the-bug.56132/page-2#post-353562
     
    Last edited: Jul 14, 2015
  2. imbault

    imbault Well-Known Member Licensed User

    First DirInternal is not the place I would put the DB, it could be deleted in case of uninstall... I would use File.DirDefaultExternal

    I don't know it's a really a bug, but I should load the image file with a Initialize, like this
    Code:
    Image.Initialize(File.DirAssets, "Image.jpg")
    And Scope of SQL1 to be private, will exclude it from others modules..., so I would put it Public
     
  3. ArminKH

    ArminKH Well-Known Member

    we should not store an private variable on Process_Global because this sub is for public variables which we want to be accessable from another activity
    Code:
    Public Sql1 as SQL
    IS Correct i think
    and also i think we should not store an ui element on process_global because it will be kept alive when the activity is recreated and maybe be a reason for memory leak
     
    Last edited: Jul 13, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    We are not there yet...

    ActuallySQL objects should always be declared as process global variables.
    It is also completely fine to declare it a private variable.
    Bitmaps are not "activity objects". It is better to declare them as process global variables.

    It really depends on your app requirements. In most cases it is better not to leave any file on the device after your app is uninstalled. Also note that File.DirDefaultExternal folder will also be cleaned when your app is uninstalled.

    LoadBitmap is identical to Bitmap.Initialize.

    This again depends on the requirements. If the SQL object will only be used in the current activity then it is better to declare it as a private variable.

    Hint: The image will be used from other activities as well.
     
    ArminKH likes this.
  5. ArminKH

    ArminKH Well-Known Member

    so maybe we should use LoadBitmapSample instead LoadBitmap?I'm not sure exactly this can be a bug or not
     
  6. sorex

    sorex Expert Licensed User

    Image.jpg should be image.jpg ?
     
    ArminKH likes this.
  7. ArminKH

    ArminKH Well-Known Member

    why?!
     
  8. sorex

    sorex Expert Licensed User

    wasn't there a limitation that some things only worked with lower cased filenames?
     
    ArminKH likes this.
  9. ArminKH

    ArminKH Well-Known Member

    no i test it with upercase name and still works
    maybe the bug be in Initialize and is related to Activity LifeCycle
     
  10. eurojam

    eurojam Well-Known Member Licensed User

    I am missing the file copy from File.DirAssets to File.DirInternal:
    Code:
    File.Copy(File.DirAssets,"1.db"File.DirInternal,"1.db")
     
    ArminKH likes this.
  11. sorex

    sorex Expert Licensed User

    maybe he want to start with an empty one?
     
    eurojam and ArminKH like this.
  12. ArminKH

    ArminKH Well-Known Member

    no,then a new database will be created if it dosn't exist any database named 1.db on DirInternal and if CreateIsNecessary is true
     
    eurojam likes this.
  13. eurojam

    eurojam Well-Known Member Licensed User

    yes you are right, I missed that:oops:
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    LoadBitmapSample is indeed better than LoadBitmap in many cases. However lets assume that the image file is at the exact required size.
     
    ArminKH likes this.
  15. sorex

    sorex Expert Licensed User

    I don't have a 5.x device but on 2.x-4. file.dirinternal was read only, not? so that would lead to an init error.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    No. Don't confuse File.DirInternal with File.DirAssets.
     
    ArminKH likes this.
  17. eurojam

    eurojam Well-Known Member Licensed User

    ....if running the code from Erel, there will be no error on my nexus, there is only this wonderfull app, doing nothing beside creating and opening a database and an image....mmmhhhh....:confused:
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    This is exactly the problem with this bug. It will almost never reveal itself during development. However it will easily crash your app when it runs on the users phone.

    Let me repeat the hint I posted:
    The image (as Main.Image) will be used from other activities as well.
     
    ArminKH likes this.
  19. sorex

    sorex Expert Licensed User

    I just copied it to a project aswell, no errors and the image displayed fine.
     
  20. sorex

    sorex Expert Licensed User

    Image is an existing type in java or something?
     
Loading...