Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

How to handle this thread?

In My application there is one thread running in activity A while it is call. Now i am going to another activity B with doing nothing to that thread. There are also another thread on activity B. Now after finishing on activity on activity B. I come back to the activity A.

At that time i got error that the thread on activity A is already running. So, what should i have to do to handle this error ??? Every time my application get crashed while i come back from activity B to A.

Thanks.

In this Error log Drawing surface is my Activity A as said above. Error Log:

11-10 15:08:24.730: ERROR/AndroidRuntime(1145): FATAL EXCEPTION: main
11-10 15:08:24.730: ERROR/AndroidRuntime(1145): java.lang.IllegalThreadStateException: Thread already started.
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at java.lang.Thread.start(Thread.java:1322)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:113)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.os.Looper.loop(Looper.java:123)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at java.lang.reflect.Method.invokeNative(Native Method)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at java.lang.reflect.Method.invoke(Method.java:521)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-10 15:08:24.730: ERROR/AndroidRuntime(1145):     at dalvik.system.NativeStart.main(Native Method)

DrawingSurface class where i got Error:

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
private Boolean _run;
protected 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);

                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(){
    return mBitmap;
}


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);
        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);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}

}

Upvotes: 0

Views: 1339

Answers (2)

JimmyB
JimmyB

Reputation: 12610

This won't work (most of the time -> race condition):

   thread.start(); // error at this line
   if(!thread.isAlive()) thread.start();

Don't do that. A thread may only be started once and there is definitely no reason to re-try starting it.

thread.start(); is really all you will ever need to start a thread.

EDIT:

while (retry) {
  thread.join();
  retry = false;
}

does not make any sense. Just join() and possibly relay the interruption to your thread.

Upvotes: 0

Optimus
Optimus

Reputation: 2776

Instead of using Thread and Runnable, if you go with AsyncTask, they will close themselves automatically after you navigate away from an activity.

or if you want to use Thread only, you'll have to use t.interrupt() to stop any running Thread t before moving away from the activity.

so, I guess put t.interrupt() in onPause() method of the activity to stop the Thread

Edit: this line is redundunt

if(!thread.isAlive())            
             thread.start();  

and i dont understand what you are trying to do with this,

thread.setRunning(true);

this might be creating some problems remove this and try again

i.e. try this

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
        thread.start(); // error at this line 
}

Upvotes: 1

Related Questions