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 8908007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:04:17+00:00 2026-06-15T03:04:17+00:00

I have an map activity for displaying a map in fullscreen mode: public class

  • 0

I have an map activity for displaying a map in fullscreen mode:

public class MapActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Panel(this));
}

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);

        // initiate new thread for drawing
        _thread = new CanvasThread(getHolder(), this);
}

// start thread 
@Override
public void surfaceCreated(SurfaceHolder holder) {
        CanvasThread.setRunning(true);
        _thread.start();
}

// start drawing objects on canvas
@Override
public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
     /* several canvas operations ...
     */
}

// Thread specific class
static class CanvasThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Panel _panel;
    private static boolean _run = false;

    public CanvasThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public static void setRunning(boolean run) {
        _run = run;
    }

    @Override
    public void run() {
        Canvas c;
        Log.d("Thread", "start");
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    Log.d("Thread", "Canvas c init");
                    _panel.onDraw(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) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}
}

Now, I’d like to use the same class to display my map in an other activity within an SurfaceView Element. Therefore I tried to take SurfaceView Element in a new Activity as follows:

....
Panel surface = new KarteActivity().new Panel(this);
    surface._thread.start();
    CanvasThread.setRunning(true);
    LinearLayout midLL = new LinearLayout(this);

    midLL.findViewById(R.id.surfaceView1);
    midLL.addView(surface);

This causes a nullpointer exception because the canvas is null. In fullscreen mode I have no errors.
What is my mistake?

How is the usual way to solve such problem?

  • 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-06-15T03:04:18+00:00Added an answer on June 15, 2026 at 3:04 am

    Ok, finally I got the answer on my own 🙂 Here is the solution:

    1. The activity which contains the SurfaceView element has to hand over the SurfaceHolder

      SurfaceView surface = (SurfaceView) findViewById(R.id.surfaceView1);
      SurfaceHolder surfaceHolder = surface.getHolder();
      Panel panel = new Panel(this,surfaceHolder);
      
    2. The surface class has to initialize the surface in its contructor and the thread will start when the surface is created.

      public class Panel extends SurfaceView implements SurfaceHolder.Callback {
      
      public Panel(Context context, SurfaceHolder surfaceHolder) {
          super(context);
          surfaceHolder.addCallback(this);
      }
      
      // start thread 
      @Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
          // initiate new thread for drawing
          _thread = new CanvasThread(surfaceHolder, this);
          _thread.setRunning(true);
          _thread.start();
      }
      
      // start drawing objects on canvas
      @Override
      public void onDraw(Canvas canvas) {
          super.onDraw(canvas);
       /* several canvas operations ...
       */
      }
      
      // Thread specific class
      class CanvasThread extends Thread {
          private SurfaceHolder _surfaceHolder;
          private Panel _panel;
          private boolean _run = false;
      
          public CanvasThread(SurfaceHolder surfaceHolder, Panel panel) {
              _surfaceHolder = surfaceHolder;
              _panel = panel;
          }
      
          public void setRunning(boolean run) {
              _run = run;
          }
      
          @Override
          public void run() {
              Canvas c;
              Log.d("Thread", "start");
              while (_run) {
                  c = null;
                  try {
                      c = _surfaceHolder.lockCanvas(null);
                      synchronized (_surfaceHolder) {
                          Log.d("Thread", "Canvas c init");
                          _panel.onDraw(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) {
                          _surfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
              }
          }
      }
      }
      

    This worked for me but I decided to seperate the class Panel out of the class MapActivity (shown in my question)

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

Sidebar

Related Questions

public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
I am working on a project that extends the Map Activity class once.This project
I have an activity MyActivity that extends from MapActivity. In the .xml file containing
I have an Activity which extends MapActivity (to show a MapView) and contains a
I have a mapview class which extends MapActivity. The code i'm using follows the
I have this MapActivity class below showing a map and putting a pin of
can i use fragments and map view in the same activity. I have seen
I have a single activity app that uses a PagerAdapter. In the OnCreate event
I have a Map activity in a fairly complex layout. As such, I can't
I've got an app using MapActivity.onCreate() to initialize the map and show it on

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.