13-7-12 android如何通过单击开始线程和停止线程

通过单击事件开启线程后,再单击就会出现:

if(camThread != null){  //如果光 camThread.start()程序会报出camThread have started
	if(camThread.isAlive())return;
}					
camThread.start();

错误如下:

 

07-12 15:21:08.545: D/AndroidRuntime(9938): Shutting down VM
07-12 15:21:08.545: W/dalvikvm(9938): threadid=1: thread exiting with uncaught exception (group=0x40a361f8)
07-12 15:21:08.545: E/AndroidRuntime(9938): FATAL EXCEPTION: main
07-12 15:21:08.545: E/AndroidRuntime(9938): java.lang.IllegalThreadStateException: Thread already started.
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at java.lang.Thread.start(Thread.java:1045)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at com.homectrl.ui.Camera$clickBtn.onClick(Camera.java:149)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.view.View.performClick(View.java:3511)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.view.View$PerformClick.run(View.java:14105)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.os.Handler.handleCallback(Handler.java:605)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.os.Handler.dispatchMessage(Handler.java:92)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.os.Looper.loop(Looper.java:137)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-12 15:21:08.545: E/AndroidRuntime(9938): 	at dalvik.system.NativeStart.main(Native Method)

错误,那怎么办,解决办法;

private Thread T1 = null;
if(T1 != null){   
	if(T1.isAlive())return;
}
T1 = new Thread(camThread);
T1.start();

每次调用的时候从新实例化线程。这样每次start的时候就不会出现异常。


 

你可能感兴趣的:(thread,thread,start,androd,already)