Exception locking surface - lockCanvas() returns a null?

by mcmc » Sat, 04 Apr 2009 01:32:35 GMT


Sponsored Links
 Everytime it hits the "c = mHolder.lockCanvas();" line, it pops up
with that same error mentioned below.

Also, I realize that c returns a null after called lockCanvas(), but
mHolder is still active. Why might this be?


I'm getting the following error as seen from the logcat window:

E/SurfaceComposerClient(  608): eLocked set when entering lock_layer
(), layer=1 (lcblk=0x410480a0), state=00000022
E/SurfaceHolder(  608): Exception locking surface
E/SurfaceHolder(  608): java.lang.IllegalArgumentException
E/SurfaceHolder(  608):         at
android.view.Surface.lockCanvasNative(Native Method)
E/SurfaceHolder(  608):         at android.view.Surface.lockCanvas
(Surface.java:190)
E/SurfaceHolder(  608):         at android.view.SurfaceView
$2.internalLockCanvas(SurfaceView.java:549)
E/SurfaceHolder(  608):         at android.view.SurfaceView
$2.lockCanvas(SurfaceView.java:528)


This is the basis of my code, which is primarily based on the
lunarview:

@Override
public void run() {
    while (mRun) {
        Canvas c = null;
        try {
            mHolder = getHolder();      //make sure holder is updated
            c = mHolder.lockCanvas();
            synchronized (mHolder) {
                if (mMode == STATE_RUNNING && c != null){
                    doDraw(c);
                }
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                mHolder.unlockCanvasAndPost(c);
            }
        }
    }
}


Can someone get back to me ASAP, this is urgent.

Thanks a lot,
mcmc

--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by Streets Of Boston » Sat, 04 Apr 2009 01:38:48 GMT


 I don't have a direct answer for you.
But this is what i usually do when problems baffle me. I browse the
git-source of Android.

Try to find the source of SurfaceHolder and find the lockCanvas( ) and
see why it may throw an exception.



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Mon, 06 Apr 2009 02:15:57 GMT


 Might also be worth checking that you aren't drawing into the surface
from a different thread, and that you aren't drawing before the
surfaceCreated() callback
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Tue, 07 Apr 2009 02:48:24 GMT


 actually, I am drawing onto the surface from a different thread...
that's not allowed?? how would I go around doing this?

I'm not drawing before the surfaceCreated() callback...

On Apr 5, 11:15am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Tue, 07 Apr 2009 08:38:14 GMT


 i, now, combined the two threads into one, and I still get the same
error at lockCanvas().

any ideas would be much appreciated. thank you.




--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Tue, 07 Apr 2009 20:34:42 GMT


 Oh - I think I can see the problem in your code now. You've got your
'finally' block outside of your while loop, rather than inside it.
This means that you are calling lockCanvas() repeatedly without
calling UnlockCanvasAndPost(). The exception is going to be thrown on
the 2nd iteration of the while loop, as you are calling lockCanvas()
when the canvas is already locked.

--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Wed, 08 Apr 2009 03:25:34 GMT


 my finally is inside my while loop.
but maybe i'm not doing it correctly...
do I have to call unlockCanvasAndPost every time I call lockCanvas
(even if it returns a null)? or do I only call unlockCanvasAndPost if
the lockCanvas returns a non null canvas (which is my current
implementation)?

On Apr 7, 5:34am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Wed, 08 Apr 2009 03:28:39 GMT


 my finally is inside my while loop.
but maybe i'm not doing it correctly...
do I have to call unlockCanvasAndPost every time I call lockCanvas
(even if it returns a null)? or do I only call unlockCanvasAndPost if
the lockCanvas returns a non null canvas (which is my current
implementation)?

On Apr 7, 5:34am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Wed, 08 Apr 2009 19:04:46 GMT


 Sorry, I misread your code before. It looks the same as lunarview and
also the same as my game loop, both of which work fine, so I think it
must be something you are doing outside of this code snippet.

You do have to unlock for every successful lock (but not if you get a
null back), so your code looks correct to me in this regard

The error looks like the one you would get if the canvas was already
locked when you called lock, so are you sure you aren't doing
something elsewhere in the code that is locking the canvas?

Maybe you could also add some more trace info - is the unlock call
definitely being reached, and is the exception thrown the 1st time
round the loop or after a number of iterations?
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Thu, 09 Apr 2009 02:01:29 GMT


 The exception is thrown the 1st time around the loop. And Unlock is
never called, since there was never a successful Lock in the first
place... :(

Right now, my code looks like this one:
 http://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/graphics/GLSurfaceView.html 

However, in the run() method, instead of having:

                if ((w > 0) && (h > 0)) {
                    /* draw a frame here */
                    mRenderer.drawFrame(gl);

                    /*
                     * Once we're done with GL, we need to call
swapBuffers()
                     * to instruct the system to display the rendered
frame
                     */
                    mEglHelper.swap();
                }

I modified it a little:

                               if ((w > 0) && (h > 0)) {
                                        mRenderer.drawFrame(gl);

                                        if (mRun){
                                                Canvas c = null;
                                                try {
                                                    mHolder = getHolder();      
//make sure holder is updated
                                                    c = mHolder.lockCanvas();
                                                    synchronized (mHolder) {
                                                        if (mMode == 
STATE_RUNNING && c != null){
                                                            doDraw(c);
                                                        }
                                                    }
                                                } finally {
                                                    // do this in a finally so 
that if an exception is thrown
                                                    // during the above, we 
don't leave the Surface in an
                                                    // inconsistent state
                                                    if (c != null) {
                                                        
mHolder.unlockCanvasAndPost(c);
                                                    }
                                                }
                                        }

                                         mEglHelper.swap();
                                }


On Apr 8, 4:04am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Thu, 09 Apr 2009 17:32:07 GMT


 I hadn't realised you are doing openGL stuff. I've used surfaceview/
holder in the same was as the lunarlander, but I haven't used openGL
so I don't know how that works.

But, at a glance, it looks like you've got a mix of two different
approaches here - maybe the lockcanvas/unlockcanvasandpost approach is
not appropriate when using openGL as the openGL code is controlling
and locking the surface?
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Fri, 10 Apr 2009 04:45:21 GMT


 wow thanks so much for your help. I think you're right! :D

however, now I run into another problem. Do you know how to convert
openGL to simply using the standard android/java 2D APIs?

On Apr 9, 2:31am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Sat, 11 Apr 2009 01:01:05 GMT


 thank you sooo much, everyone!
i've been meaning for someone to tell me that since a very long time
ago, but no one did, so i was stuck!

:)





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Instal hampir semua program ke SD card tanpa root (2.2)

Iya
Confirm dari terminal juga begitu bro. 
Kalo di custom rom rodri misalnya,dia nambahin icon berbentuk sdcard untuk set 
install location dimana. 

@yopiesuryadi
The Next Big Thing is Coming....

-----Original Message-----
From: "yuk...@gmail.com" <yuk...@gmail.com>
Sender: id-android@googlegroups.com
Date: Fri, 16 Jul 2010 03:02:25 
To: id-android<id-android@googlegroups.com>
Reply-To: id-android@googlegroups.com
Subject: [id-android] Instal hampir semua program ke SD card tanpa root (2.2)

INi tadi baru iseng coba2 buka shell
dan nemu:

jalankan perintah

pm setInstallLocation 2

di shell (pake adb shell, atau mungkin bisa juga pake terminal emulator
seperti connectbot? tapi belum ngetes)

Maka, kalau ke settings -> applications -> manage applications,
app2 yang tadinya "Move to SD Card"nya ga nyala, sekarang jadi nyala!

Asik, tadinya sisa 30 MB skarang jadi 60 MB lebih (pake n1).

-- 
"Indonesian Android Community [id-android]" 

2. FileNotFound Exception on Device not Emulator using URLConnection

I"m doing a basic HTTP Get. Works fine a bagillion times in the
emulator.

Fails with this on the Device. I can load the URL fine in the browser
on the device.

W/System.err( 4576): java.io.FileNotFoundException: http://gotfanz.com/v1/join
W/System.err( 4576):    at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:
1061)

Odd
?

-- 

3. Windows unable to recognize the HTC evo device

4. cannot install USB drivers for HTC Slide, Android 2.1, Windows Vista 32 bit

5. Help: service auto restarts on breakpoint when debugging a service

6. Building Android Library Projects from the Command Line

7. Widget problems after application update.