android - 在Android中,处理程序逻辑

  显示原文与译文双语对照的内容

下面是我在处理程序中使用的代码,以便创建一个单独的线程。 我有一个列表,它存储所有方格,我只希望 10显示( 用于测试目的) 使用. size() 来获得列表中的平方数。

这个想法是随机生成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 

时间: 作者:

你已经为 Activity 实现了一个构造函数,并将它放在 onCreate 中。

这里错误与处理程序无关。

作者:
...