下面是我在处理程序中使用的代码,以便创建一个单独的线程。 我有一个存储所有Squares的列表,我只想使用.size()来显示(用于测试目的)10来获取列表中的方块数。
这个想法是随机生成x和y值然后.addSquare会将它添加到列表中,但也可以在我的画布上绘制。
我的问题是,我如何延迟每个方格抽签的时间,以便所有10个不同时出现?
编辑:我的意思是,延迟显示每个人。
感谢。
handler = new Handler();
Runnable runnable = new Runnable()
{
@Override
public void run() {
while (sModel.getSquares().size() < 10 )
{
int width = 980;
int height = 1280;
// create random
Random rand = new Random();
// float values for storing random x, y coords
float randX, randY;
randX = rand.nextInt(width); // random x
randY = rand.nextInt(height); // random y
sModel.addSquare(randX, randY, Color.BLUE);
handler.postDelayed(this, 2000);
}
}
};handler.postDelayed(runnable,1000);
编辑:按要求提供LogCat :(尝试实施wait()和notify()时出错)
01-15 07:22:47.811: W/asset(3951): Copying FileAsset 0x6a1eeb58 (zip:/data/app/ce0941a.coursework.Int.BoxesGame-1.apk:/resources.arsc) to buffer size 5184 to make it aligned.
01-15 07:22:47.901: I/Adreno-EGL(3951): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL3907963)
01-15 07:22:47.901: I/Adreno-EGL(3951): OpenGL ES Shader Compiler Version: 17.01.09
01-15 07:22:47.901: I/Adreno-EGL(3951): Build Date: 09/26/13 Thu
01-15 07:22:47.901: I/Adreno-EGL(3951): Local Branch:
01-15 07:22:47.901: I/Adreno-EGL(3951): Remote Branch:
01-15 07:22:47.901: I/Adreno-EGL(3951): Local Patches:
01-15 07:22:47.901: I/Adreno-EGL(3951): Reconstruct Branch:
01-15 07:22:47.991: D/qdmemalloc(3951): ion: Mapped buf base:0x6c932000 size:8355840 offset:0 fd:54
01-15 07:22:48.001: D/qdmemalloc(3951): ion: Mapped buf base:0x6c874000 size:4096 offset:0 fd:55
01-15 07:22:52.526: D/qdmemalloc(3951): ion: Mapped buf base:0x6d26d000 size:8355840 offset:0 fd:57
01-15 07:22:52.526: D/qdmemalloc(3951): ion: Mapped buf base:0x6c886000 size:4096 offset:0 fd:58
01-15 07:22:52.636: D/qdmemalloc(3951): ion: Mapped buf base:0x6da65000 size:8355840 offset:0 fd:59
01-15 07:22:52.636: D/qdmemalloc(3951): ion: Mapped buf base:0x6c887000 size:4096 offset:0 fd:60
01-15 07:22:52.636: W/dalvikvm(3951): threadid=1: thread exiting with uncaught exception (group=0x415d4970)
01-15 07:22:52.636: E/AndroidRuntime(3951): FATAL EXCEPTION: main
01-15 07:22:52.636: E/AndroidRuntime(3951): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ce0941a.coursework.Int.BoxesGame/ce0941a.coursework.controller.GameActivity}: java.lang.InstantiationException: can't instantiate class ce0941a.coursework.controller.GameActivity; no empty constructor
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2435)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.os.Looper.loop(Looper.java:158)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.main(ActivityThread.java:5789)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.reflect.Method.invoke(Method.java:525)
01-15 07:22:52.636: E/AndroidRuntime(3951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-15 07:22:52.636: E/AndroidRuntime(3951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
01-15 07:22:52.636: E/AndroidRuntime(3951): at dalvik.system.NativeStart.main(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): Caused by: java.lang.InstantiationException: can't instantiate class ce0941a.coursework.controller.GameActivity; no empty constructor
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.Class.newInstanceImpl(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.Class.newInstance(Class.java:1130)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
01-15 07:22:52.636: E/AndroidRuntime(3951): ... 11 more
01-15 07:22:59.263: D/Process(3951): killProcess, pid=3951
01-15 07:22:59.263: D/Process(3951): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:123 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
答案 0 :(得分:1)
您已为您的活动实施了一个构造函数,并将其放在onCreate
中。
此错误与处理程序无关。