Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:28:30+00:00 2026-05-31T00:28:30+00:00

When testing several games with a standard game thread in Android 4.0 it works

  • 0

When testing several games with a standard game thread in Android 4.0 it works well until the Activity is shut down (pressing the home button etc), leaving the activity the application crashes with a nullPointer.

This even happens in the sample LunarLander that Google has programmed.

The problem is that Canvas becomes null when leaving the activity and that makes the application crash.

The error message from the LogCat is just below.

02-27 18:07:58.974: V/MainThread(2667): CANVAS android.view.Surface$CompatibleCanvas@4102bcf0
02-27 18:07:59.164: V/MainThread(2667): CANVAS null
02-27 18:07:59.164: W/dalvikvm(2667): threadid=14: thread exiting with uncaught exception (group=0x409c01f8)
02-27 18:07:59.174: E/AndroidRuntime(2667): FATAL EXCEPTION: Thread-108
02-27 18:07:59.174: E/AndroidRuntime(2667): java.lang.NullPointerException
02-27 18:07:59.174: E/AndroidRuntime(2667):     at com.joakimengstrom.pong.MainThread.run(MainThread.java:49)

This is the code when starting a thread, with the Log.v you see above.

    while(this.running){
        canvas = null;

        try {               
            canvas = this.surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder) {
                Log.v(TAG, "CANVAS " + canvas);
                canvas.drawColor(Color.BLACK);
                ...

And below is when creating the thread and shutting it down when the surface is destroyed.

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread = new MainThread(getHolder());
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    thread.setRunning(false);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {

        }
    }
}

How do I exit the thread in a safe way without making canvas null?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:28:32+00:00Added an answer on May 31, 2026 at 12:28 am

    In Android 4.0, SurfaceHolder.lockCanvas can return null when the surface is destroyed. So here:

    try {               
                canvas = this.surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    Log.v(TAG, "CANVAS " + canvas);
                    canvas.drawColor(Color.BLACK);
    

    Just surround your synchronized (surfaceHolder) block with if (canvas != null). This should not cause any issues with the behaviour of your application as it occurs when drawing is not needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read several questions about testing android apps on device. I know possibility to
Since performance of string concatenation is quite weak in VB6 I'm testing several StringBuilder
When testing our software on several different systems (98-XP-Vista-Seven-Linux-etc), I think that the best
I have several web services I need to create for them some testing mechanism
I am testing a web app that writes cookies to subdomain.thisdomain.com and several subfolders
I've seen other people mention several types of testing on Stack Overflow. The ones
If I am testing several different defined parameters can I call multiple xsl:when statements
I have several files with code testing code (which uses a unittest class). Later
I see several other questions about load testing web services. But as far as
New to unit testing and have an app that is spread out over several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.