在我的应用程序中,在调用时,活动A中有一个运行的线程。现在我要去另一个活动B而不对该线程做任何事情。 活动B上还有另一个主题。 现在完成活动B的活动后,我回到活动A。
那时我得到了活动A上的线程已经运行的错误。 那么,我该怎么办才能处理这个错误? 每次我的应用程序崩溃,而我从活动B回到A。
感谢。
在此错误日志中,绘图表面是我的活动A,如上所述。 错误日志:
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类,我得到错误:
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...
}
}
}
}
答案 0 :(得分:1)
如果您使用Thread
,则不会使用Runnable
和AsyncTask
,而是在您离开活动后自动关闭。
或者如果您只想使用Thread
,则必须先使用t.interrupt()
停止任何正在运行的Thread
t
,然后再离开活动。
所以,我想将t.interrupt()
放在活动的onPause()
方法中以停止Thread
编辑:此行是冗余
if(!thread.isAlive())
thread.start();
并且我不明白你想要做什么,
thread.setRunning(true);
这可能会造成一些问题,请将其删除并重试
即。试试这个
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.start(); // error at this line
}
答案 1 :(得分:0)
这不起作用(大部分时间 - >竞争条件):
thread.start(); // error at this line
if(!thread.isAlive()) thread.start();
不要那样做。线程只能以一次启动,并且肯定有没有的理由重新尝试启动它。
thread.start();
真的是你需要创建一个线程。
编辑:
while (retry) {
thread.join();
retry = false;
}
没有任何意义。只需join()
并可能将中断转发给您的thread
。