【发布时间】:2011-12-21 19:00:44
【问题描述】:
我在一个活动名称 DrawingActivity 中有从 android 库中选择图像的按钮。还有另一个活动名称为 DrawingSurface。在 DrawingSurface 活动中,我使用线程来绘制画布并处理它。
现在,当我要从图库中提取图片时,出现如下错误:
11-04 12:12:50.226: ERROR/AndroidRuntime(518): FATAL EXCEPTION: main
11-04 12:12:50.226: ERROR/AndroidRuntime(518): java.lang.IllegalThreadStateException: Thread already started.
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at java.lang.Thread.start(Thread.java:1322)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:106)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.os.Looper.loop(Looper.java:123)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 12:12:50.226: ERROR/AndroidRuntime(518): at dalvik.system.NativeStart.main(Native Method)
===========================
我在 DrawingSurface 活动中遇到此错误的行如下:
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start(); // error at this line
}
我正在使用此代码在按下按钮时选择图像:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
//startActivity(intent);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
//finish();
那么现在有朋友可以告诉我如何处理这个错误吗?
谢谢。
【问题讨论】:
-
我认为你正在启动一个已经在运行的线程。
-
是的,看起来是这样。那么我该如何处理呢?如果是的话。
-
从logcat看来问题是因为你调用了thread.start()两次。一个线程无法启动两次,这就是异常出现的原因。我注意到在logcat中,有一个'at android.view.SurfaceView.onWindowVisibilityChanged',你是否关闭了窗口并再次打开它?也许这导致了系统调用“surfaceCreated(SurfaceHolder holder)”并导致了这个问题。我想你应该确保在“public abstract void surfaceDestroyed (SurfaceHolder holder)”中停止线程。
-
你得到我的答案了吗?试试看?
标签: android multithreading android-layout canvas android-emulator