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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:36+00:00 2026-05-26T20:25:36+00:00

This is My Class where i am implement the thread to draw on canvas.

  • 0

This is My Class where i am implement the thread to draw on canvas.

    package com.example.drawing;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.drawing.DrawingActivity;

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
    public static Boolean _run;
    public static DrawThread thread;
    public Canvas canvas = null;
    private CommandManager commandManager;
    //private Bitmap myBitmap;
    private Bitmap mBitmap;

    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

        getHolder().addCallback(this);

        commandManager = new CommandManager();
        thread = new DrawThread(getHolder());
    }

    class DrawThread extends Thread{
        private SurfaceHolder mSurfaceHolder;

        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;

        }

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

        @Override
        public void run() {
            //Canvas canvas = null;
            while (_run){

                try{

                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }

                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(Color.WHITE); 

//                    Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                      canvas.drawBitmap (kangoo, 0,  200,null);

//                  works for logo                  
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                  c.drawBitmap (kangoo, 0,  200,null);

                    if(!(DrawingActivity.imagePath==null)){
                        c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
                    }
                    commandManager.executeAll(c);
                    canvas.drawBitmap (mBitmap, 0,  0,null);

                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    public void addDrawingPath (DrawingPath drawingPath){
        commandManager.addCommand(drawingPath);
    }

    public boolean hasMoreRedo(){
        return commandManager.hasMoreRedo();
    }

    public void redo(){
        commandManager.redo();
    }

    public void undo(){
        commandManager.undo();
    }

    public boolean hasMoreUndo(){
        return commandManager.hasMoreRedo();
    }

    public Bitmap getBitmap(){
        Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.resize_drawing_logo);
        Bitmap myBitmap = addLogo(mBitmap, kangoo);
        return myBitmap;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
        // TODO Auto-generated method stub
        mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
            //thread.setRunning(true); // Edited By Shreyash
            _run=true; //
            thread.start();
             // error at this line 
//          if(!thread.isAlive())            
//               thread.start(); 
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        //thread.setRunning(false); //Edited By Shreyash
        _run = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
    public static Bitmap addLogo(Bitmap mainImage, Bitmap logoImage) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 

        Bitmap finalImage = null; 
        int width, height = 0; 
        width = mainImage.getWidth(); 
        height = mainImage.getHeight(); 
        finalImage = Bitmap.createBitmap(width, height, mainImage.getConfig()); 
        Canvas canvas = new Canvas(finalImage); 
        canvas.drawBitmap(mainImage, 0,0,null);
        canvas.drawBitmap(logoImage, canvas.getWidth()-logoImage.getWidth() ,canvas.getHeight()-logoImage.getHeight() ,null);

        return finalImage; 
    }


}

I am creating the Object of this class in to another activity and do drawing.
Now while i am going to another activity and then come back to this activity, then the error is occurs.

Error log:

11-15 11:26:40.738: ERROR/AndroidRuntime(668): FATAL EXCEPTION: main
11-15 11:26:40.738: ERROR/AndroidRuntime(668): java.lang.IllegalThreadStateException: Thread already started.
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.Thread.start(Thread.java:1322)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:119)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.os.Looper.loop(Looper.java:123)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.reflect.Method.invoke(Method.java:521)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at dalvik.system.NativeStart.main(Native Method)
11-15 11:26:40.768: WARN/ActivityManager(60):   Force finishing activity com.example.drawing/.DrawingActivity
11-15 11:26:41.338: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{450073d0 com.example.drawing/.DrawingActivity}

I donr know why i am getting this error.
So please help me to handle this thread.
Thanks.

  • 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-26T20:25:37+00:00Added an answer on May 26, 2026 at 8:25 pm

    Well, it’s exactly as it says: you’re calling start() on a thread which has already been started. It’s very unusual to want to create a thread in one section of code and then start it in another… if you can keep the two together, this problem can’t happen.

    (As an aside, it looks like your thread is basically tight-looping, which doesn’t seem like a good idea to me, but that’s a different matter.)

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

Sidebar

Related Questions

Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
i have this class called MemoryManager, it is supposed to implement a simple smart
If I try to implement my class on this file I get an error
I am writing a class to implement an algorithm. This algorithm has three levels
What Swing class can I use to implement something like this? Add To List
I have this question: Does implementing a custom MembershipProvider class needs you to implement
I have been looking to implement a custom class of : IList<ArraySegment<byte>> this will
I'd like to use .NET 's Lazy<T> class to implement thread safe caching. Suppose
I've created a class which is an extension of Thread. This class hits a

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.