Android Question admob and canvas initialize

dagodom

Member
Licensed User
Longtime User
Hi

I have a strange error:

i have two activity (main e playzone)

on main i have

B4X:
 StartActivity(playzone)


in second activity i have

B4X:
Sub Activity_Create(FirstTime As Boolean)

   AdView1.Initialize("Ad", ".......................")
   Activity.AddView(AdView1, 0dip, DipToCurrent(admobtop), DipToCurrent(admobwidth), 50dip)  
   AdView1.LoadAd 
End Sub

Sub Activity_Resume
...
   pz_canvas1.Initialize(panel1)
   pz_canvas3.Initialize(panel3)
...
end sub
if i back to main and restart playzone activity several times (six or seven) i have this error


Installing file.
PackageAdded: package:com.regrid.beta
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = true **
** Activity (playzone) Resume **
** Activity (playzone) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = false **
** Activity (playzone) Resume **
** Activity (playzone) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = false **
** Activity (playzone) Resume **
** Activity (playzone) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = false **
** Activity (playzone) Resume **
** Activity (playzone) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = false **
** Activity (playzone) Resume **
** Activity (playzone) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (playzone) Create, isFirst = false **
** Activity (playzone) Resume **
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)
at com.regrid.beta.playzone._set_grid_new(playzone.java:3410)
at com.regrid.beta.playzone._activity_resume(playzone.java:599)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at com.regrid.beta.playzone.afterFirstLayout(playzone.java:104)
at com.regrid.beta.playzone.access$100(playzone.java:16)
at com.regrid.beta.playzone$WaitForLayout.run(playzone.java:76)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)


at line 3410 i have

mostCurrent._pz_canvas1.Initialize((android.view.View)(mostCurrent._panel1.getObject()));


if i drop off admob (i use 2.0 library) everything works fine

i try in several way to overcome problem but result is the same, with admob crash, without all is well

Someone can help me ?

thanks
 
Last edited:

dagodom

Member
Licensed User
Longtime User
same error

at com.regrid.beta.playzone._activity_create(playzone.java:470)

line 470

mostCurrent._pz_canvas1.Initialize((android.view.View)(mostCurrent._panel1.getObject()));
 
Upvote 0

dagodom

Member
Licensed User
Longtime User
canvas are over transparent panel and panel are positioning over a grid of label
bitmap can be transparent ?
 
Upvote 0

dagodom

Member
Licensed User
Longtime User
Thanks a lot for answer.

I am a little confused but i study and try
some help about "how drawing the current background to the bitmap" ?
 
Upvote 0

dagodom

Member
Licensed User
Longtime User
I put initialize canvas on activity create of second activity
all is well if i back and return with button, but if i press back key on second activity, return to first activity and restart second activity
sub activity_create is call.
So, if i don't initialize canvas i have error at first canvas DrawRect
If initialize canvas after six time i have memory error.
may be i put code in wrong routine.

first activity
B4X:
Sub Process_Globals
   Dim b1,b3 As Bitmap
End Sub

second activity
B4X:
Sub Globals
   Dim vb1, vb3 As ImageView
   Dim canvas1, canvas3 As Canvas  
End Sub

Sub Activity_Create(FirstTime As Boolean)

LayoutVal = GetDeviceLayoutValues      
'If FirstTime == True Then
    Main.b1.InitializeMutable(LayoutVal.width, LayoutVal.Height)
    canvas1.Initialize2(Main.b1)
    vb1.Initialize("") 'initialize the imageview
    vb1.Bitmap = Main.b1
    Activity.AddView(vb1,0,0,LayoutVal.width, LayoutVal.Height) 
    Main.b3.InitializeMutable(LayoutVal.width, LayoutVal.Height)
    canvas3.Initialize2(Main.b3)
    vb3.Initialize("") 'initialize the imageview
    vb3.Bitmap = Main.b3
    Activity.AddView(vb3,0,0,LayoutVal.width, LayoutVal.Height) 
'   End If   
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
See this code:
B4X:
Sub Process_Globals
   Dim ivBackground As Bitmap
End Sub

Sub Globals
   Dim iv As ImageView
   Dim cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ivBackground.InitializeMutable(300dip, 300dip)
   End If
   iv.Initialize("iv")
   Activity.AddView(iv, 0, 0, 300dip, 300dip)
   iv.SetBackgroundImage(ivBackground)
   cvs.Initialize2(ivBackground)
   cvs.DrawColor(Colors.White) 'clear previous drawings
End Sub
It uses a single bitmap that is preserved when the activity is destroyed.
 
Upvote 0

dagodom

Member
Licensed User
Longtime User
i put code but now all operation with canvas drawing nothing
i put instruction for draw circle for try but nothing appear

B4X:
If FirstTime == True Then
   b1.InitializeMutable(LayoutVal.width, LayoutVal.Height)
   b3.InitializeMutable(LayoutVal.width, LayoutVal.Height)   
End If     
vb1.Initialize("")
Activity.AddView(vb1,0,0,LayoutVal.width, LayoutVal.Height) 
vb1.SetBackgroundImage(b1)
canvas1.Initialize2(b1)
canvas1.DrawCircle(100,100,20,Colors.black,True,1)
'canvas1.DrawColor(Colors.White)

i think is another problem (my problem) in the code
but now i understand ....
Thanks Erel
 
Upvote 0
Top