Width and height must be > 0

589 views
Skip to first unread message

Steve Marcus

unread,
Jan 25, 2014, 7:12:02 AM1/25/14
to alternate-java-bridg...@googlegroups.com
Any ideas on this one? 

I am sporadically getting this error, when using canvas. I cannot pinpoint it unfortunately.

java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:724)
at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:516)
at com.xiledsystems.AlternateJavaBridgelib.components.altbridge.CanvasView.processSizeChange(CanvasView.java:279)
at com.xiledsystems.AlternateJavaBridgelib.components.altbridge.CanvasView.onLayout(CanvasView.java:249)

I am specifying width and height in the layout xml file to "match_parent":

 <com.xiledsystems.altbridge.eclipse.AB_Canvas
        android:id="@+id/canvasScreen1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

 Do I also need to set the width and height programmatically as well?

Thanks for any help.


S

Imp Inc

unread,
Feb 7, 2014, 10:09:47 PM2/7/14
to alternate-java-bridg...@googlegroups.com
Sorry for missing this. Did you set an image background for it?

Steve Marcus

unread,
Feb 9, 2014, 8:44:50 PM2/9/14
to alternate-java-bridg...@googlegroups.com
Sorry for the late reply. I have been informed its not a canvas issue, it's a bitmap issue. I can't pinpoint it as I have loads of bitmaps and it doesn't say where in the app it is occurring.

So in hindsight I'm not sure you can actually help. I will have to try and hunt it down somehow I think. Any ideas where to look?

Imp Inc

unread,
Feb 11, 2014, 3:03:52 PM2/11/14
to alternate-java-bridg...@googlegroups.com
Well, it could be a Canvas issue.

Do you set a background image on the canvas?

Steve Marcus

unread,
Mar 9, 2014, 1:37:26 AM3/9/14
to alternate-java-bridg...@googlegroups.com
Hey,

Sorry I've been busy relocating to another country. Think I found it!

I was using DrawCircle on a canvas to draw different coloured circles of increasing size, using a clock.

private void clock1Timer() {
CircleRadius += 20;
canvasHyperspace.PaintColor(COLOR_DKGRAY);
canvasHyperspace.DrawCircle(canvasHyperspace.Width()/2, canvasHyperspace.Height()/2, CircleRadius);
canvasHyperspace.PaintColor(COLOR_GRAY);
canvasHyperspace.DrawCircle(canvasHyperspace.Width()/2, canvasHyperspace.Height()/2, CircleRadius/2);
canvasHyperspace.PaintColor(COLOR_LTGRAY);
canvasHyperspace.DrawCircle(canvasHyperspace.Width()/2, canvasHyperspace.Height()/2, CircleRadius/4);
canvasHyperspace.PaintColor(COLOR_WHITE);
canvasHyperspace.DrawCircle(canvasHyperspace.Width()/2, canvasHyperspace.Height()/2, CircleRadius/8);
}

However, the clock has a very fast interval, and I started the clock running (timer enabled = true) in $define()
clock1 = new Clock(this);
clock1.TimerInterval(50); 
clock1.TimerEnabled(true);

And I did not set the canvasHyperspace.Width() and canvasHyperspace.Height() until screenInitialized()! So it could possibly process canvasHyperspace.DrawCircle(canvasHyperspace.Width()/2, canvasHyperspace.Height()/2, CircleRadius) before it ran screenInitialized()?

Do you think this is what the problem could be?




Imp Inc

unread,
Mar 10, 2014, 10:20:05 PM3/10/14
to alternate-java-bridg...@googlegroups.com
Oh nice, if you don't mind my asking where'd ya move to/from?

It's possible, but I'm not sure it is the issue.

The canvas has it's own initialization (it seems to sometimes take longer to initialize than other things). Events.CANVAS_INIT can be captured to know when the Canvas is ready to be drawn on.

You also said this isn't something that happens all the time, right? Could it be when orientation changes?

Ryan

Steve Marcus

unread,
Mar 11, 2014, 5:57:33 PM3/11/14
to alternate-java-bridg...@googlegroups.com
Thanks Ryan,

Moved from London to New Zealand! Beautiful here.

Hmm you don't sound convinced that was the issue... Orientation changes should not be the issue as I have overidden rotation on all activities to be one orientation only.

Yes I cannot get this error to occur on my phone at all, but it is happening to the odd person.

Some more information if it helps:

I have 5 activities ('screens') with a canvas.


2 do not have any background image. 1 uses .DrawCircle to draw different coloured circles of increasing size, as described above.


1 has a background image which is set in $define:

canvasGameOver = new Canvas(this, R.id.canvasGameOver);

canvasGameOver.BackgroundImage("planet_gameover");


1 has a background image which is set in a procedure

private void setCanvasOn() {

canvasScreen1.BackgroundImage("canvasscreen1bg.png");

}

and then removed in another procedure

private void setCanvasOff() {

canvasScreen1.BackgroundImage("");

}


1 has a background image set in ScreenInitialize:

canvasSectorMap.BackgroundImage("bgmap"+MapSector+".png");

and then it is removed in onDestroy

public void onDestroy() {

super.onDestroy();

canvasSectorMap.BackgroundImage("");

}

After this, and on each activity, in onDestroy I am calling this unbindDrawables procedure, this was to get rid of the OutOfMemoryErrors:

public void unbindDrawables(View view) {

    try{

           if (view.getBackground() != null) {

               ((BitmapDrawable)view.getBackground()).getBitmap().recycle();

               view.getBackground().setCallback(null);

               view=null;

           }


           if (view instanceof ViewGroup) {

               for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

               unbindDrawables(((ViewGroup) view).getChildAt(i));

               }

           ((ViewGroup) view).removeAllViews();

           }


   }catch (Exception e) {

       // TODO: handle exception

       e.printStackTrace();

   }

}   


Could setting the canvas backgroundImage to ("") and then calling this procedure afterwards be the problem?




Imp Inc

unread,
Mar 11, 2014, 10:31:56 PM3/11/14
to alternate-java-bridg...@googlegroups.com
It's hard to tell what the issue is exactly. Can you tell which activity the error is happening on? Do you run bugsense at all?

Ryan

Steve Marcus

unread,
Mar 12, 2014, 6:18:00 AM3/12/14
to alternate-java-bridg...@googlegroups.com
Yes I agree very hard. I can't tell where it is happening. 

Never mind, I will monitor it and see if my fix worked. What is bugsense?

Hal Abelson

unread,
Mar 12, 2014, 10:46:53 AM3/12/14
to Imp Inc, alternate-java-bridg...@googlegroups.com
I haven't been following this thread, so I don't know what the original error was.   But there appears to be an order of events issue where Canvas can try to allocate a bitmap that is of size zero because the correct size has not been computed.

I recently made a change to Canvas.java to protect against this error.   That change has been merged into our MIT master branch.   See the try/catch block in Canvas.java at line 412 and the catch at line 439.


==Hal


--
You received this message because you are subscribed to the Google Groups "Java Bridge Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to alternate-java-bridge-libr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Imp Inc

unread,
Mar 12, 2014, 12:54:39 PM3/12/14
to alternate-java-bridg...@googlegroups.com, Imp Inc
Thanks Hal. I'll look into it. It sounds like it may be the issue.

Steve,

 Bugsense is a library you can add to your project to report crashes. www.bugsense.com to check it out. It's very handy to be able to see all the crashes that happen, so you don't have to hope that the user sends the error report to Google. It's very simple to set up. I'd highly recommend it, or something like it.

Ryan

==Hal


To unsubscribe from this group and stop receiving emails from it, send an email to alternate-java-bridge-library-discussion+unsubscribe@googlegroups.com.

Hal Abelson

unread,
Mar 12, 2014, 1:13:32 PM3/12/14
to Imp Inc, alternate-java-bridg...@googlegroups.com
There seem to be several things that can tickle that order of events bug.  Another one is having a canvas of size fill parent inside a nest of hori or vert arrangements

==Hal


To unsubscribe from this group and stop receiving emails from it, send an email to alternate-java-bridge-libr...@googlegroups.com.

Steve Marcus

unread,
Mar 13, 2014, 9:45:47 PM3/13/14
to alternate-java-bridg...@googlegroups.com
Thanks so much Hal and Ryan for putting in the fix. I haven't had any further crash reports so fingers crossed it did the trick.
Reply all
Reply to author
Forward
0 new messages