2014-01-15 76 views
2

下面是我在處理程序中使用的代碼,以創建單獨的線程。 我有一個存儲所有方塊的列表,我只希望顯示10個(用於測試目的)使用.size()來獲取列表中的方塊數。Android中的處理程序邏輯

這個想法是隨機生成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 
+1

你可以在當前線程中調用wait並在10秒後調用notifyAll –

+0

@Karan_Rana我對Android編程非常陌生,請你舉個例子嗎?謝謝。 – RevXaisks

+0

http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 此鏈接可能會清除您的主題概念 –

回答

1

你已經實現了你的活動構造刪除,並把它的代碼在onCreate

此錯誤與處理程序無關。

+0

LogCat發佈後,我試圖從Karan_Rana的建議顯示我的執行wait()和notify()以顯示我的錯誤,當它沒有工作。 代碼沒有錯誤,它運行良好。 我需要延遲處理程序中繪製正方形所需的時間。 有什麼建議嗎? 非常感謝。 – RevXaisks

+0

當前代碼發生了什麼? –