我想實現一個無限循環,因爲我想要代碼一次又一次運行follwing代碼......但它導致我的應用程序崩潰.. 。正如我在這個高科技初學者,我不知道這...任何幫助將appreciated..thank你..while(true)loop迫使我的應用程序崩潰在Android
這裏是logcat的
06-20 22:04:38.029 22848-22848/sensor.swapni E/AndroidRuntime: FATAL
EXCEPTION: main
Process:
sensor.swapni, PID: 22848
java.lang.OutOfMemoryError: Failed to allocate a 13571692 byte allocation
with 7716072 free bytes and 7MB until OOM
at
java.util.ArrayList.add(ArrayList.java:118)
at
android.view.ViewRootImpl$RunQueue.postDelayed(ViewRootImpl.java:8656)
at
android.view.View.postDelayed(View.java:14275)
at
sensor.swapni.Second.onCreate(Second.java:54)
at
android.app.Activity.performCreate(Activity.java:6904)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at
android.app.ActivityThread.access$1100(ActivityThread.java:229)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:148)
at
android.app.ActivityThread.main(ActivityThread.java:7325)
at
java.lang.reflect.Method.invoke(Native Method)
at
這裏是java代碼
while(true) {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);
}
}, 1000);
tv1.postDelayed(new Runnable() {
@Override
public void run() {
tv2.setVisibility(View.INVISIBLE);
tv1.setVisibility(View.VISIBLE);
}
}, 2000);
您可以發佈您收到的錯誤嗎? @Alex – cunniemm
你在這裏試圖做什麼 – Tony
這將是因爲你的循環執行速度遠遠超過了android可以處理所有這些任務,所以任務會填滿隊列,最終這會失敗並出現某種QueueOverflow異常,或者你的JVM會只是耗盡內存。 –