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.
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.)